【发布时间】:2015-11-03 07:02:43
【问题描述】:
我有服务
@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
消费者在扔
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
虽然product 类只有 2 个参数 qty,只有 name。
【问题讨论】:
-
它说没有阅读器(提供者)。你有 JSON 提供者吗?如果没有,您应该添加它。见here。选择您的 RESTeasy 版本
-
没有帮助,我已经添加了
-
你添加了jar还是依赖?一个罐子是不够的。
-
@peeskillet - 添加了使用的罐子的屏幕截图
-
点击我提供的链接。向下滚动。您将看到四个杰克逊罐子的列表。单击版本按钮,它将带您到一个页面,您可以通过单击下载按钮下载 jar。下载所有四个并添加它们。如果它仍然不起作用,请告诉我。您可能还需要显式注册提供程序。
标签: java json web-services rest