泽西岛客户端引发MessageBodyProviderNotFoundException

时间:2018-09-11 20:23:41

标签: jersey jax-rs moxy

我有一个Jersey客户端,该客户端引发MessageBodyProviderNotFoundException 在我的示例中,我可以使用moxy解析从文件读取的JSON,然后尝试使用Jersey客户端解析相同的JSON

我检查了由moxy解析创建的WarehouseResponse实例,它看起来很完美。

我的依赖项

  <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>com.ca.Plex</groupId>
        <artifactId>ObRun</artifactId>
        <version>7.2.1.002</version>
        <scope>system</scope>
        <systemPath>C:/ProgramData/CA/Plex/7.2.1/ObJava/lib/ObRun.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.27</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
    </dependency>
  </dependencies>

示例代码

public class PlexExample {

    public static void main(String[] args) throws Exception {
        Class<?>[] ctx = {WarehousesResponse.class, WarehouseResponse.class, WarehouseFields.class};
        JAXBContext jc = JAXBContext.newInstance(ctx);
        Unmarshaller um = jc.createUnmarshaller();
        um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
        um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        Source json = new StreamSource(new File("D:\\CM_First\\Pacorini\\Workspace\\Resources\\warehouses.json"));
        WarehousesResponse warehouses = um.unmarshal(json, WarehousesResponse.class).getValue();
        ObCharFld url = new ObCharFld("http://localhost:8080/HSyncREST007/api/v1");
        ObCharFld path = new ObCharFld("warehouses");
        warehouses = executeGET(url, path);
    }

    public static WarehousesResponse executeGET(ObCharFld obUri, ObCharFld obPath) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(obUri.getValue())
                .path(obPath.getValue());
        Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
        Response response = builder.get();
        return  response.readEntity(WarehousesResponse.class); // Exception thrown here
    }
}

堆栈跟踪

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/x-json;charset=UTF-8, type=class com.pacorini.rest.client.WarehousesResponse, genericType=class com.pacorini.rest.client.WarehousesResponse.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:232)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:321)
    at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:389)
    at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:264)
    at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
    at com.pacorini.rest.client.PlexExample.executeGET(PlexExample.java:41)
    at com.pacorini.rest.client.PlexExample.main(PlexExample.java:32)

1 个答案:

答案 0 :(得分:0)

服务器将内容类型设置为text / x-json。 得到响应后,我设置响应头。

public static WarehousesResponse executeGET(ObCharFld obUri, ObCharFld obPath) {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(obUri.getValue())
            .path(obPath.getValue());
    Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
    Response response = builder.get();
    response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    return  response.readEntity(WarehousesResponse.class); 
}