users@jersey.java.net

Re: [Jersey] JSON Validation

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Tue, 6 Apr 2010 17:42:02 -0700 (PDT)

Hi Jakub,

I have very belatedly logged a bug and submitted a test case demoing the problem. Better late than never :).

It's here:

https://jersey.dev.java.net/issues/show_bug.cgi?id=499

Thanks,

Charles



----- Original Message ----
From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
To: users_at_jersey.dev.java.net
Sent: Thu, March 25, 2010 2:58:36 AM
Subject: Re: [Jersey] JSON Validation


Hi Charles,

On Wed, Mar 24, 2010 at 10:51:40AM -0700, Charles Overbeck wrote:
> Hi Jakub,
>
> The stack trace follows. I'll put this together as a standalone zip file that demonstrates the problem in the next day or two. Thanks for your help!

That would be great! Thanks for the stack trace.

~Jakub

>
> javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
> - with linked exception:
> [org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'contactType'.]
> at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:107)
> at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:444)
> at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:137)
> at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
> at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:119)
> at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:147)
> at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
> at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
> at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
> at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
> at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
> at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
> at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
> at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
> at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
> at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
> at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
> at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:797)
> at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:770)
> at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:731)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
> at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
> at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
> at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
> at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
> at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
> at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
> at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
> at java.lang.Thread.run(Thread.java:595)
>
> Caused by: javax.xml.bind.UnmarshalException
> - with linked exception:
> [org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'contactType'.]
> at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:425)
> at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)
> at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339)
> at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:103)
> at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:92)
> at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:100)
> at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:105)
>
> ... 37 more
>
> Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'contactType'.
> at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
> at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
> at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
> at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
> at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
> at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
> at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
> at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:89)
> at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
> at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:242)
> at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
> at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
>
> ... 42 more
>
>
> Charles
>
>
> ________________________________
> From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
> To: users_at_jersey.dev.java.net
> Sent: Wed, March 24, 2010 2:04:48 AM
> Subject: Re: [Jersey] JSON Validation
>
>
> Hello Charles,
>
> please see inline....
>
> On Mon, Mar 22, 2010 at 06:19:31PM -0700, Charles Overbeck wrote:
> > Hello,
> >
> > I have a Jersey Resource with the method below. ContactType is a JAXB generated class from an XSD. It just has two fields, name and age.
> >
> > @PUT
> > @Path("{id: [0-9]+}")
> > @Consumes({MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
> > @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
> > public JAXBElement<ContactType> updateContact(ContactType contactType, @PathParam("id") Long id) {
> > final ObjectFactory objectFactory = new ObjectFactory();
> > return objectFactory.createContact(contactType); // Just a test; not doing anything yet.
> > }
> >
> > Great, I can submit both XML and JSON! But if I pass in bad field name, e.g., my JSON looks like this: {"name":"John","ige":"15"}, the name is initialized, but the "ige" is ignored. Same happens if my XML is wrong. I would prefer an error occur.
> >
> > So I add a ContextResolver:
> >
> > @Provider
> > public class DemoContextResolver implements ContextResolver<Unmarshaller> {
> >
> > ...
> > public Unmarshaller getContext(Class<?> type) {
> > try {
> > Unmarshaller unmarshaller = JAXBContext.newInstance("mypackage").createUnmarshaller(); // I know I should cache this. It's like this for this message.
> > unmarshaller.setSchema(schema); // schema points to my XSD.
> > return unmarshaller;
> > } catch (JAXBException e) {
> > return null;
> > }
> > }
> > }
> >
> > Now the XML validation works as I expect. If I PUT an XML document with a bad element name, I get a SAXParseException, which I want.
> >
> > But now my JSON doesn't work at all, good or bad. I get "[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'contactType'.]"
>
> Could you please send me the stack trace?
>
> >
> > In the getContext() method I also tried "new JSONJAXBContext(JSONConfiguration.natural().build(), ContactType.class).createUnmarshaller()", but to no avail.
> >
> > Am I missing something obvious here? I suspect I am. None of the JSON examples that I found quite match up exactly with what I'm doing... Any thoughts or help is appreciated.
>
> When implementing the JSON API, we have not thought about validating JSON using the XML schema validation
> of the intermediary XML data, but i think it is generally not a bad idea for the JAXB/JSON scenario.
>
> Regarding the current behavior, even if we put aside the JSON validation, i think you should be able to validate
> XML wthout breaking the JSON functionality.
> Could you please file a bug and attach your zipped application as a test case?
>
> Thanks,
>
> ~Jakub
>
> >
> > Thanks in advance,
> >
> > Charles
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
http://blogs.sun.com/japod
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net