У меня есть службаRestful службы аргумент несовпадение исключение
@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)
WHe ре, как веб-сервис бросает
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 Params только qty
, name
только.
В нем говорится, что нет читателя (провайдера). У вас есть провайдер JSON? Если нет, вы должны добавить его. См. [Здесь] (http://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider). Выберите версию RESTeasy –
did not help, у меня было уже добавлено – Varun
Вы добавили банку или зависимость? Одной банки недостаточно. –