Unable to find a MessageBodyReader of content-type text/html and type interface java.util.List

余生长醉 提交于 2019-12-08 04:12:58

问题


Please do not consider it as duplicate request as i have gone through all the post in stackoverflow, none of them answered. no response i found.

Issue is :

I am facing a very weird issue making a GET request with same piece of code via standalone application (inside main()). It works where as if the same code is put in Java EE app and deployed on a wildfly 10 server, it gives Error

"Unable to find a MessageBodyReader of content-type text/html and type interface java.util.List"

Code:

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 10000);
configuration.property(ClientProperties.READ_TIMEOUT, 10000);
Client iexRestClient=ClientBuilder.newClient(configuration);
WebTarget webTarget = 
iexRestClient.target("https://api.iextrading.com/1.0/ref-data/symbols/");
Response response = 
webTarget.request().accept(MediaType.APPLICATION_JSON).get(Response.class);
System.out.println("response status "+response.getStatus());
List<IEXTicker> tickers = response.readEntity(new 
GenericType<List<IEXTicker>>(){});
System.out.println("size of tickers "+tickers.size());

Required jars(pom.xml file)

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.26</version>
</dependency>

Model class : IEX_Ticker.java

@Entity
@Table(name = "IEX_Ticker")
@JsonIgnoreProperties(ignoreUnknown = true)
public class IEXTicker implements Serializable {


    private static final long serialVersionUID = 1L;

    public IEXTicker() {
        super();
    }

    @Id
    @Column(name = "symbol", length = 20)
    private String symbol;

    @Column(name = "name", length = 500)
    private String name;

    @Column(name = "date", length = 20)
    private String date;

    @Column(name = "isEnabled")
    private boolean isEnabled;

    @Column(name = "type", length = 20)
    private String type;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }


    public boolean isEnabled() {
        return isEnabled;
    }

    public void setEnabled(boolean isEnabled) {
        this.isEnabled = isEnabled;
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String toString() {
        return "Symbol :" + symbol + " Name : " + name + " Date : " + date + " IsEnabled : 
        "+isEnabled+" Type:
        "+type;
    }

}

Error :

2017-10-31 20:32:17 ERROR stderr:71 - javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/html and type interface java.util.List  
2017-10-31 20:32:17 ERROR stderr:71 - javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/html and type interface java.util.List  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:75)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:52)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)  
2017-10-31 20:32:18 ERROR stderr:71 -   at  org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:225)  
2017-10-31 20:32:18 ERROR stderr:71 -   at com.swigel.iex.api.IEXTickerOperation.populateIEXTickerData1(IEXTickerOperation.java:127)  
2017-10-31 20:32:18 ERROR stderr:71 -   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
2017-10-31 20:32:18 ERROR stderr:71 -   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
2017-10-31 20:32:18 ERROR stderr:71 -   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
2017-10-31 20:32:18 ERROR stderr:71 -   at java.lang.reflect.Method.invoke(Unknown Source)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)  
2017-10-31 20:32:18 ERROR stderr:71 -   at  org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)  
2017-10-31 20:32:18 ERROR stderr:71 -   at  org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)  
2017-10-31 20:32:18 ERROR stderr:71 -   at  org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)  
2017-10-31 20:32:18 ERROR stderr:71 -   at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)

回答1:


I had the same problem. It was caused by the server which did not, as expected, return a content-type: application/json, but it returned:

content-type: text/html; charset=UTF-8

I see two options:

1) contact the provider of the service and ask that application/json be returned

2) read the result of your request as a String and manually convert it to an object. Similar to this:

String jsonData = response.readEntity(String.class);
ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
JsonNode jsonNode= mapper.readValue(jsonData, com.fasterxml.jackson.databind.JsonNode.class);

Now, create a List from the jsonNode data...



来源:https://stackoverflow.com/questions/47040557/unable-to-find-a-messagebodyreader-of-content-type-text-html-and-type-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!