JAXRS 客户端 - Pojo 的反序列化问题
JAXRS client - Deserialization Issue with Pojo
大家好。我是 Jersey 和 Jackson 的新手,发现很难在客户端反序列化来自我的 REST 服务的 JSOn 响应。我很确定我仍然没有很好地掌握对象映射器和 JSON 提供程序 API。提前感谢任何帮助或指导。这是我的源代码。
源代码
POJOClass
@XmlRootElement(name = "user")
public class User implements Serializable{
private String firstName;
private String lastName;
private String email;
private String userID;
public User() {
}
public User(String firstName, String lastName, String email, String userID) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.userID = userID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
客户代码
package com.example.service.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;
import com.example.service.bean.User;
import com.example.service.client.mapper.MyMessageBodyReader;
import com.example.service.client.mapper.MyObjectMapperProvider;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
User user = target.request(MediaType.APPLICATION_JSON).get(User.class);
System.out.println("User : " + user.getUserID());
}
}
`
前 2 个 sysout 生成的输出为
200
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha@us.ibm.com","userID":"nishiz"}}
但是当我试图直接从响应中获取用户对象时,我得到一个错误
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@10163d6; line: 1, column: 10] (through reference chain: com.example.service.bean.User["user"])
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:806)
at org.glassfish.jersey.client.JerseyInvocation.access0(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
at com.example.service.client.GetJSONResponse.main(GetJSONResponse.java:40)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
如果你们中的任何人能指导我如何解决这个问题,那就太好了。
我没有使用 Maven,因为我最初想尝试不使用 Maven
我不确定为什么我的休息服务正在包装响应。这是代码:
服务方式
@GET
@Path("/{username}")
@Produces(MediaType.APPLICATION_JSON)
public User helloWorld(@PathParam("username") String name){
User user = new User();
user.setFirstName("Nishit");
user.setLastName("Ladha");
user.setUserID("nishiz");
user.setEmail("ladha@us.ibm.com");
return user;
}
Web.xml##
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example.service,
com.fasterxml.jackson.jaxrs.json
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
伙计,看,你正在尝试绑定不存在的字段 User。
如果你想正确解析这个 json
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha@us.ibm.com","userID":"nishiz"}}
你需要有类似的东西class
public class UserWrapper implements Serializable{
private User user;
// Constructors
// Getters, and setters
// HashCode and equals
}
那么这个客户端代码就可以工作了:
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
UserWrapper userWrapper = target.request(MediaType.APPLICATION_JSON).get(UserWrapper.class);
}
}
如果您有任何问题 - ask.Hope 您的代码可以正常工作。
大家好。我是 Jersey 和 Jackson 的新手,发现很难在客户端反序列化来自我的 REST 服务的 JSOn 响应。我很确定我仍然没有很好地掌握对象映射器和 JSON 提供程序 API。提前感谢任何帮助或指导。这是我的源代码。
源代码
POJOClass
@XmlRootElement(name = "user")
public class User implements Serializable{
private String firstName;
private String lastName;
private String email;
private String userID;
public User() {
}
public User(String firstName, String lastName, String email, String userID) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.userID = userID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
客户代码
package com.example.service.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;
import com.example.service.bean.User;
import com.example.service.client.mapper.MyMessageBodyReader;
import com.example.service.client.mapper.MyObjectMapperProvider;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
User user = target.request(MediaType.APPLICATION_JSON).get(User.class);
System.out.println("User : " + user.getUserID());
}
}
` 前 2 个 sysout 生成的输出为
200
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha@us.ibm.com","userID":"nishiz"}}
但是当我试图直接从响应中获取用户对象时,我得到一个错误
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@10163d6; line: 1, column: 10] (through reference chain: com.example.service.bean.User["user"])
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:806)
at org.glassfish.jersey.client.JerseyInvocation.access0(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
at com.example.service.client.GetJSONResponse.main(GetJSONResponse.java:40)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
如果你们中的任何人能指导我如何解决这个问题,那就太好了。
我没有使用 Maven,因为我最初想尝试不使用 Maven
我不确定为什么我的休息服务正在包装响应。这是代码:
服务方式
@GET
@Path("/{username}")
@Produces(MediaType.APPLICATION_JSON)
public User helloWorld(@PathParam("username") String name){
User user = new User();
user.setFirstName("Nishit");
user.setLastName("Ladha");
user.setUserID("nishiz");
user.setEmail("ladha@us.ibm.com");
return user;
}
Web.xml##
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example.service,
com.fasterxml.jackson.jaxrs.json
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
伙计,看,你正在尝试绑定不存在的字段 User。 如果你想正确解析这个 json
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha@us.ibm.com","userID":"nishiz"}}
你需要有类似的东西class
public class UserWrapper implements Serializable{
private User user;
// Constructors
// Getters, and setters
// HashCode and equals
}
那么这个客户端代码就可以工作了:
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
UserWrapper userWrapper = target.request(MediaType.APPLICATION_JSON).get(UserWrapper.class);
}
}
如果您有任何问题 - ask.Hope 您的代码可以正常工作。