Restful service argument mismatch exception

2019-07-11 15:03发布

问题:

I have a service

@POST
    @Path("/post")
    @Consumes("application/json")
    public Response createProductInJSON(Product product) {
        String result = "Product created : " + product;
        return Response.status(201).entity(result).build();
    }

and the consumer

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());
        }

taken from link

consumer is throwing

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 500
at com.mkyong.rest.Consumer.main(Consumer.java:52)

where as the web service is throwing

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 class has 2 params only qty, name only.

回答1:

The error message is

Could not find message body reader for type: class com.mkyong.rest.Product of content type: application/json

This means that either you do not have a MessageBodyReader that can handle JSON or you have one and it is not registered.

In your case, you have the resteasy-jackson-provider, but it is still dependent on the rest of the core Jackson libraries, which are

  • 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. 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.

For anyone else, that is not using the 2.2.1 version of RESTeasy, you should select the correct Jackson version that goes with your RESTeasy version. You can see here and select the version of RESTeasy you are using.

Also note that the links I provided are for Jackson 1.x version, as that's the version that resteasy-jackson-provider uses. If you are using a newer 3.x version of RESTeasy, there is also Jackson 2.x support in the `resteasy-jackson2-provider



回答2:

It looks like 'application/json' could not be resolved

 @Consumes(MediaType.APPLICATION_JSON)

Make sure you have add the right dependency

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