gpt4 book ai didi

Java JsonMappingException 错误

转载 作者:行者123 更新时间:2023-11-30 11:38:11 28 4
gpt4 key购买 nike

我已经使用 jersey 在 java 中编写了一个 REST API。当我使用 XML 请求时,一切正常,但当我使用 JSON 请求时,我总是得到 JsonMappingException。

这些对象是使用来自 xsd 文件的 Jaxb 生成的。由于 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>

最佳答案

注意:我是 EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导和成员专家组。

JAXB (JSR-222) 规范没有说明任何关于 JSON 绑定(bind)的内容。 JAX-RS 提供者利用的几个 JSON-Binding 实现能够从 JAXB 注释中派生出 JSON 表示。在您的环境中,您正在使用 Jackson . Jackson 支持 JAXB 元数据的子集,目前不支持 JAXBElement 类。请参阅 Jackson 领导的以下回答:

EclipseLink JAXB (MOXy) 为我们的 JSON 绑定(bind)实现中的所有 JAXB 结构提供支持,它可能更适合您的用例:

下面是一些使用 MOXy 作为 JSON 提供程序的 Jersey 官方示例的链接:

关于Java JsonMappingException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709602/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com