为什么``readEntity(MyObj.class)`失败,而`objectMapper.readValue(str,MyObj.class);`工作原理

时间:2016-09-08 12:38:22

标签: java json jackson

我有这段代码

import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
/**
 * Created by eladb on 9/6/16.
 */
public class DeploymentClient {

    private CommonClient commonClient;


    String appName = "topic-test-publisher";


    public DeploymentClient(String url, InputStream clientSecretFile) {
        commonClient = new CommonClient(url, clientSecretFile);
    }

    public VersionOld getCurrentConfigVersion(String project, String subDir) throws Exception {
        VersionOld version = commonClient.authorizedRequestBuilder(commonClient.webTarget
                .path("/apps/get_current_version/default/"+appName+"/"+appName)
                .queryParam("object_type", "app"))
                .accept(MediaType.APPLICATION_JSON_TYPE)
                .get(ClientResponse.class)
                .readEntity(VersionOld.class);

        return null;
    }
}

给了我那个例外:

javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Map type [map type; class javax.ws.rs.core.MultivaluedMap, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]

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)

但是此代码有效:

import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
/**
 * Created by eladb on 9/6/16.
 */
public class DeploymentClient {

    private CommonClient commonClient;


    String appName = "topic-test-publisher";


    public DeploymentClient(String url, InputStream clientSecretFile) {
        commonClient = new CommonClient(url, clientSecretFile);
    }

    public VersionOld getCurrentConfigVersion(String project, String subDir) throws Exception {
        String str = commonClient.authorizedRequestBuilder(commonClient.webTarget
                .path("/apps/get_current_version/default/"+appName+"/"+appName)
                .queryParam("object_type", "app"))
                .accept(MediaType.APPLICATION_JSON_TYPE)
                .get()
                .readEntity(String.class);



        VersionOld v =  foo(str);
        return null;
    }

    public VersionOld foo(String str){
            ObjectMapper objectMapper = new ObjectMapper();
        VersionOld v = null;
        try {
            v = objectMapper.readValue(str, VersionOld.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
    }
}

在两种情况下,http响应都是:

str = {"versions": {"ap": "Not Set", "am": "topic-test-publisher-1.0.16", "il": "topic-test-publisher-1.0.16", "row": "topic-test-publisher-1.0.49"}, "provider": "gce"}

为什么这段代码能正常工作?

它的逻辑是否相同?

1 个答案:

答案 0 :(得分:0)

只需将ClientResponse替换为客户端代码中的javax.ws.rs.core.Response,或者根本不使用任何参数。

    VersionOld version = commonClient.authorizedRequestBuilder(commonClient.webTarget
            .path("/apps/get_current_version/default/"+appName+"/"+appName)
            .queryParam("object_type", "app"))
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(javax.ws.rs.core.Response.class)//or get().readEntity(VersionOld.class);
            .readEntity(VersionOld.class);

我相信您使用了 org.glassfish.jersey.client.ClientResponse ,但返回的资源可能是 javax.ws.rs.core.Response (已经发生了对我而言)

我使用以下服务器端代码

进行了测试
@GET
@Path("/us")
@Produces(MediaType.APPLICATION_JSON)
public Response getJson() { //javax.ws.rs.core.Response
    VersionOld vo = new VersionOld();
    vo.versions.put("ap", "Not set");
    vo.versions.put("am", "topic-test-publisher-1.0.16");
    vo.versions.put("il", "topic-test-publisher-1.0.16");
    vo.versions.put("row", "topic-test-publisher-1.0.49");
    vo.provider = "gce";


    return Response.status(200).entity(vo).build();
}

,POJO定义为

public class VersionOld {

  //public variables but you can use getters/setters
public Map<String,String> versions = new HashMap<>();
public String provider;

public VersionOld(){}
  }  

输出

Msg: topic-test-publisher-1.0.16
Msg: topic-test-publisher-1.0.49