Jersey客户端抛出MessageBodyProviderNotFoundException

问题描述 投票:0回答:1

我有一个抛出MessageBodyProviderNotFoundException的Jersey客户端在我的例子中,我可以使用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)
jersey jax-rs moxy
1个回答
1
投票

服务器将内容类型设置为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); 
}
© www.soinside.com 2019 - 2024. All rights reserved.