我正在尝试编写一个简单的Resteasy客户端来访问mt rest web服务。不幸的是我收到错误:
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)
客户代码:
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);
休息服务代码:
@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!");
}
客户端代码响应正常。即200.相同的网址与Mozilla Rest客户端一起工作正常。任何帮助将不胜感激
答案 0 :(得分:1)
确保您的客户端项目中包含所有jackson库。如果你正在使用maven,你应该这样:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>xxx</version>
</dependency>
编辑: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
您还可以尝试在客户端实现中设置接受内容类型
request.accept("application/json");
答案 1 :(得分:0)
添加jettison
提供程序jar后,问题得到解决。
我觉得Resteasy-2.3.2
有一些问题,最后我将Reateasy升级为2.3.5
,客户端工作正常。