Клиент Джерси генерирует исключение MessageBodyProviderNotFoundException - PullRequest
0 голосов
/ 11 сентября 2018

У меня есть клиент Джерси, который выдает исключение MessageBodyProviderNotFoundException В моем примере я могу проанализировать чтение JSON из файла с помощью moxy, затем я пытаюсь использовать клиент Jersey для анализа того же JSON

Я проверил экземпляр WarehouseResponse, созданный при анализе moxy, и он выглядит идеально.

Мои зависимости

  <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 голосов
/ 12 сентября 2018

Сервер устанавливал тип содержимого как 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); 
}
...