我使用 jersey 在 java 中编写了一个 REST API。当我使用 XML 请求时,一切正常,但是当我使用 JSON 请求时,我总是得到 JsonMappingException。
这些对象是使用 Jaxb 从 xsd 文件生成的。由于 XML 请求有效,我假设这些生成的对象是正确的。
谁能告诉我我错过了什么?
(在底部您可以找到错误消息、WS、对象和 xsd 示例)
提前致谢。
错误:
[#|2012-12-04T18:55:56.599+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=29;_ThreadName=Thread-2;|StandardWrapperValve[ServletAdaptor]: PWC1406: Servlet.service() for servlet ServletAdaptor threw exception
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<be.api.schema.sensorrequest.SensorCreateRequest>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream@20a49a63; line: 2, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:474)
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ajp.AjpProcessorTask.invokeAdapter(AjpProcessorTask.java:135)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
|#]
WS 实现:
@POST
@Consumes(
{
MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
})
@Produces(
{
MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
})
@Validation(requestObject = SensorCreateRequest.class, xsd = "SensorRequest.xsd")
public SensorCreateResponse post(@TypeHint(SensorCreateRequest.class) JAXBElement<SensorCreateRequest> JAXBSensorCreateRequest,
@HeaderParam(value = "client") String clientName,
@HeaderParam(value = "session") String sessionToken) throws WebApplicationException
{
}
请求对象 (SensorCreateRequest)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SensorCreateRequestType", propOrder = {
})
@XmlRootElement(name = "sensorCreateRequest")
public class SensorCreateRequest implements Serializable, Equals, HashCode, ToString
{
@XmlElement(namespace = "http://www.api.be/schema/Sensor", required = true)
protected Sensors sensors;
/**
* Default no-arg constructor
*
*/
public SensorCreateRequest() {
super();
}
/**
* Fully-initialising value constructor
*
*/
public SensorCreateRequest(final Sensors sensors) {
this.sensors = sensors;
}
//Getters, Setters, toString, hash & equals
}
传感器对象:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SensorsType", propOrder = {
"sensors"
})
@XmlRootElement(name = "sensors")
public class Sensors implements Serializable, Equals, HashCode, ToString
{
@XmlElement(name = "sensor")
protected List<Sensor> sensors;
/**
* Default no-arg constructor
*
*/
public Sensors() {
super();
}
/**
* Fully-initialising value constructor
*
*/
public Sensors(final List<Sensor> sensors) {
this.sensors = sensors;
}
//Getters, Setters, toString, hash & equals
}
传感器对象:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SensorType", propOrder = {
"sensorId",
"name",
"token",
"groups",
"locations",
"dataProfile",
"status",
"first",
"last",
"lastValue",
"longName",
"circles"
})
@XmlRootElement(name = "sensor")
public class Sensor implements Serializable, Equals, HashCode, ToString
{
protected int sensorId;
@XmlElement(required = true)
protected String name;
/**
* Default no-arg constructor
*
*/
public Sensor() {
super();
}
/**
* Fully-initialising value constructor
*
*/
public Sensor(final int sensorId, final String name) {
this.sensorId = sensorId;
this.name = name;
}
//Getters, Setters, toString, hash & equals
}
SensorRequest xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.api.be/schema/SensorRequest"
xmlns:tns="http://www.api.be/schema/SensorRequest"
xmlns:s="http://www.api.be/schema/Sensor"
elementFormDefault="qualified">
<xsd:import namespace="http://www.api.be/schema/Sensor" schemaLocation="Sensor.xsd"/>
<xsd:element name="sensorCreateRequest" type="tns:SensorCreateRequestType"/>
<xsd:complexType name="SensorCreateRequestType">
<xsd:all>
<xsd:element ref="s:sensors" minOccurs="1" maxOccurs="1" />
</xsd:all>
</xsd:complexType>
</xsd:schema>
传感器 xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.api.be/schema/Sensor"
xmlns:tns="http://www.api.be/schema/Sensor"
elementFormDefault="qualified">
<!-- Sensor -->
<xsd:element name="sensors" type="tns:SensorsType"/>
<xsd:complexType name="SensorsType">
<xsd:sequence>
<xsd:element name="sensor" type="tns:SensorType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sensor" type="tns:SensorType" />
<!-- Sensors -->
<xsd:complexType name="SensorType">
<xsd:sequence>
<xsd:element name="sensorId" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>