我有服务
@POST
@Path("/post")
@Consumes("application/json")
public Response createProductInJSON(Product product) {
String result = "Product created : " + product;
return Response.status(201).entity(result).build();
}
和消费者
url = new URL(
"http://localhost:8080/TestRestWebService/json/product/post");
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
Gson gson = new Gson();
Product p = new Product();
p.setName("varun");
p.setQty(33);
String input = gson.toJson(p).toString();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
取自link http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
消费者正在扔
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 500
at com.mkyong.rest.Consumer.main(Consumer.java:52)
Web 服务抛出的位置
SEVERE: Failed executing POST /json/product/post org.jboss.resteasy.spi.InternalServerErrorException: Bad arguments passed to public javax.ws.rs.core.Response com.mkyong.rest.JSONService.createProductInJSON(com.mkyong.rest.Product)
(org.jboss.resteasy.spi.BadRequestException org.jboss.resteasy.spi.BadRequestException: Could not find message body reader for type: class com.mkyong.rest.Product of content type: application/json )
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:181)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: argument type mismatch at
thoughproduct
类只有 2 个参数qty
, name
only.
错误信息是
找不到类型为 com.mkyong.rest.Product 的消息正文阅读器,内容类型为:application/json
这意味着您要么没有MessageBodyReader
可以处理 JSON,或者您有一个但未注册。
就您而言,您有resteasy-jackson-provider
,但它仍然依赖于杰克逊核心库的其余部分,它们是
jackson-core-asl
jackson-jaxrs
jackson-mapper-asl
jackson-xc
For your particular version of resteasy-jackson-provider:2.2.1
, the Jackson version used is 1.6.3. You can download those jars here http://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson-provider/2.2.1.GA. Just scroll down and click the 1.6.3 button for each of the jars and then click the Download (JAR) for each of those jars, and add them to your project.
对于没有使用 RESTeasy 2.2.1 版本的其他人,您应该选择与您的 RESTeasy 版本搭配的正确 Jackson 版本。你可以看到here http://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson-provider并选择您正在使用的 RESTeasy 版本。
另请注意,我提供的链接适用于 Jackson 1.x 版本,因为该版本resteasy-jackson-provider
用途。如果您使用的是 RESTeasy 的较新 3.x 版本,则还支持 Jackson 2.x`resteasy-jackson2-provider http://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)