JAXRS客户端 - Pojo的反序列化问题

时间:2015-09-22 19:51:11

标签: json rest jackson jersey-2.0 jersey-client

Helo Everyone。我是泽西和杰克逊的新手,发现很难在客户端从我的REST服务反序列化JSOn响应。我很确定我仍然没有抓住Object mapper和JSON提供程序API。任何帮助或指导都提前感谢。这是我的源代码。

源代码

POJO等级

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());           

    }

}

客户代码

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.access$700(JerseyInvocation.java:92)
    at org.glassfish.jersey.client.JerseyInvocation$2.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"])

` 前2个sysout生成输出

  

200

     

{"使用者" {"的firstName":" Nishit"" lastName的":" Ladha&#34 ;, "电子邮件":" ladha@us.ibm.com","用户ID":" nishiz"}}

但是当我尝试直接从响应中获取User对象时,我收到错误

@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;
}

如果有人能指导我如何解决这个问题,那将非常友好。

我没有使用Maven,因为我首先想要在没有Maven的情况下尝试

我不知道为什么我的休息服务正在回复。这是代码:

服务方法

<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>

Web.xml中##

{{1}}

1 个答案:

答案 0 :(得分:-1)

伙计,看,您正在尝试绑定不存在的字段用户。 如果你想正确解析这个json

{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha@us.ibm.com","userID":"nishiz"}}

您需要与此类相似

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);          

    }

}

如果您有任何疑问 - 请问。希望您的代码可以使用。