静态服务参数不匹配异常 [英] Restful service argument mismatch exception

查看:77
本文介绍了静态服务参数不匹配异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有服务

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

摘录自链接

消费者扔了

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

Web服务抛出的位置

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

though product类仅具有2个参数qty,仅name.

thoughproduct class has 2 params only qty, name only.

推荐答案

错误消息是

找不到类型为com.mkyong.rest的消息正文阅读器.内容类型为application/json的产品

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

这意味着您没有可以处理JSON的MessageBodyReader,或者您没有一个可以注册JSON的MessageBodyReader.

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

在您的情况下,您拥有resteasy-jackson-provider,但是它仍然依赖于Jackson核心库的其余部分,

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
  • jackson-core-asl
  • jackson-jaxrs
  • jackson-mapper-asl
  • jackson-xc

对于您的特定版本的resteasy-jackson-provider:2.2.1,使用的Jackson版本是1.6.3.您可以在此处下载这些罐子.只需向下滚动并为每个罐子单击 1.6.3 按钮,然后为每个罐子单击下载(JAR),然后将它们添加到您的项目中.

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.

对于其他未使用RESTeasy 2.2.1版本的用户,您应该选择与RESTeasy版本一起使用的正确Jackson版本.您可以在此处并选择要使用的RESTeasy版本

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.

还请注意,我提供的链接适用于Jackson 1.x版本,因为这是resteasy-jackson-provider使用的版本.如果您使用的是RESTeasy的3.x版本,则`resteasy-jackson2-provider

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

这篇关于静态服务参数不匹配异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆