gpt4 book ai didi

java - 如何将POST中的json转换为javaBean

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:23 25 4
gpt4 key购买 nike

这是我的提供商:

class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"

这是我的 javaBean

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonRootName;

@JsonRootName("issue")
public class TestBean {

@JsonProperty("project_id")
private Integer projectId;

@JsonProperty("subject")
private String subject;

public Integer getProjectId() {
return projectId;
}

public void setProjectId(Integer projectId) {
this.projectId = projectId;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

}

这是我的服务

 @WebService
public class IssueRestfulApi {

@POST
@Path("/create")
@Consumes(value = {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public void createIssue(TestBean bean) {
System.out.println(bean.getSubject());
System.out.println("get create request");
}
}

然后我发送这样的帖子请求:

{"issue": {
"project_id": 1,
"subject": "Example"
}
}

最后我得到了这个异常:

Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "issue" (Class com.rakuten.tranp.api.bean.TestBean), not marked as ignorable
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@783478b0; line: 1, column: 12] (through reference chain: com.rakuten.tranp.api.bean.TestBean["issue"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1311)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1262)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:801)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:764)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:212)
... 28 more

这个问题怎么解决,请帮帮我,非常感谢您的每一次,需要注意的是。

最佳答案

将此属性添加到您的 application.property 文件中,然后重试:

spring.jackson.deserialization.UNWRAP_ROOT_VALUE=true

关于java - 如何将POST中的json转换为javaBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654735/

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