简体   繁体   English

Resteasy客户端抛出异常

[英]Resteasy client throwing exception

I am trying to write a simple Resteasy client to access mt rest web service. 我正在尝试编写一个简单的Resteasy客户端来访问mt rest Web服务。 Unfortunately I am getting the error: 不幸的是我遇到了错误:

Exception in thread "main" org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type application/json and type null
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:523)
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:514)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:415)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:377)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:350)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:344)
    at com.test.client.rest.employee.EmployeeClient.main(EmployeeClient.java:29)

Client code: 客户代码:

ClientRequest request = new ClientRequest(ROOT_URL + "getEmp/GS");

    ClientResponse<Employee> resp = request.get(Employee.class);
    //Response resp = request.get();
    if(resp.getResponseStatus().getStatusCode() == 200)
    {
        System.out.println("resp ok!!!");
    }

    Employee e = resp.getEntity(Employee.class);
    System.out.println("path:" + e);

Rest Service code: 休息服务代码:

@GET
    @Path("getEmp/{name}")
    @Produces("application/json")
    public Employee getEmployee(@PathParam("name") String name)
    {
        if(em.containsKey(name))
            return em.get(name);
        else
            throw new EmployeeNotFoundException("Employee with name '" + name + "' does not exists!");
    }

Client code response is ok. 客户端代码响应正常。 ie 200. The same url works fine with Mozilla Rest client. 即200。相同的URL在Mozilla Rest客户端上可以正常使用。 Any help would be appreciated 任何帮助,将不胜感激

Make sure you have all the jackson libraries in your client project. 确保您的客户端项目中具有所有的jackson库。 If you are using maven, you should have this: 如果您正在使用Maven,则应具有以下功能:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>xxx</version>
</dependency>

EDIT: The jars used by resteasy-jaxrs and resteasy-jackson-provider are 编辑:resteasy-jaxrs和resteasy-jackson-provider使用的罐子是

[INFO] +- org.jboss.resteasy:resteasy-jaxrs:jar:2.2.1.GA:compile
[INFO] |  +- org.jboss.resteasy:jaxrs-api:jar:2.2.1.GA:compile
[INFO] |  +- org.scannotation:scannotation:jar:1.0.3:compile
[INFO] |  |  \- javassist:javassist:jar:3.12.1.GA:compile
[INFO] |  +- javax.annotation:jsr250-api:jar:1.0:compile
[INFO] |  +- javax.activation:activation:jar:1.1:compile
[INFO] |  +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.2:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.0.3:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.0.1:compile
[INFO] |  \- net.jcip:jcip-annotations:jar:1.0:compile
[INFO] \- org.jboss.resteasy:resteasy-jackson-provider:jar:2.2.1.GA:compile
[INFO]    +- org.codehaus.jackson:jackson-core-asl:jar:1.6.3:compile
[INFO]    +- org.codehaus.jackson:jackson-mapper-asl:jar:1.6.3:compile
[INFO]    +- org.codehaus.jackson:jackson-jaxrs:jar:1.6.3:compile
[INFO]    \- org.codehaus.jackson:jackson-xc:jar:1.6.3:compile

You could also try setting the accepts content type in your client implementation 您也可以尝试在客户端实现中设置接受内容类型

request.accept("application/json");

After adding the jettison provider jars, the issue got resolved. 添加了jettison提供者罐子后,问题得到解决。

Still I feel Resteasy-2.3.2 has some problem and eventually i upgraded my Reateasy to 2.3.5 and the client is working perfectly. 我仍然觉得Resteasy-2.3.2一些问题,最终我将Reateasy升级到2.3.5 ,并且客户端运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM