将JSON传递给WebService

web*_*rer 7 java json web-services dropwizard

我使用dropwizard框架在java中开发了一个web服务.我希望它消耗一个json.

我的服务代码是 -

- 资源等级

@Path(value = "/product")
  public class ProductResource{ 

   @POST
   @Path(value = "/getProduct")
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(MediaType.APPLICATION_JSON)
   public Product getProduct(InputBean bean) {
    // Just trying to print the parameters. 
    System.out.println(bean.getProductId());
    return new Product(1, "Product1-UpdatedValue", 1, 1, 1);
   }
} 
Run Code Online (Sandbox Code Playgroud)

- InputBean是一个简单的bean类.

public class InputBean {

    private int productId;
    private String productName;

    public String getProductName() {
        return productName;
    }
    public void setProductName(String productName) {
        this.productName= productName;
    }

    public int getProductId() {
        return productId;
    }
    public void setProductId(int productId) {
        this.productId= productId;
        }
}
Run Code Online (Sandbox Code Playgroud)

客户代码 -

    public String getProduct() {

            Client client = Client.create();
            WebResource webResource = client.resource("http://localhost:8080/product/getProduct");
JSONObject data = new JSONObject ("{\"productId\": 1, \"productName\": \"Product1\"}");
            ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .post(ClientResponse.class, data);
            return response.getEntity(String.class);
        }
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 -

ClientHandlerException

这段代码有什么问题吗?

JSONObject data = new JSONObject ("{\"productId\": 1, \"productName\": \"Product1\"}");
ClientResponse response =  webResource.type(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON)
                        .post(ClientResponse.class, data);
Run Code Online (Sandbox Code Playgroud)

有人可以指出我可能会缺少什么吗?

客户日志 -

在此输入图像描述

小智 0

您正确设置类型并正确发出请求。

问题是你没有什么可以处理响应。

A message body reader for Java class my.class.path.InputBean
Run Code Online (Sandbox Code Playgroud)

...基本上是说,您返回的内容无法读取、格式化和输出为任何有用的内容。

您在服务中返回一个 Product 类型,即您的八位字节流,但我看不到您在哪里有 MessageBodyWriter 将此响应输出到 JSON。

你需要:

 @Provider
@Produces( { MediaType.APPLICATION_JSON } )
public static class ProductWriter implements MessageBodyWriter<Product>
{
    @Override
    public long getSize(Product data, Class<?> type, Type genericType, Annotation annotations[], MediaType mediaType)
    {
        // cannot predetermine this so return -1
        return -1;
    }

    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
        if ( mediaType.equals(MediaType.APPLICATION_JSON_TYPE) )
        {
            return Product.class.isAssignableFrom(type);
        }

        return false;
    }

    @Override
    public void writeTo(Product data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> headers, OutputStream out) throws IOException, WebApplicationException
    {
        if ( mediaType.equals(MediaType.APPLICATION_JSON_TYPE) )
        {
            outputToJSON( data, out );
        }
    }
    private void outputToJSON(Product data, OutputStream out) throws IOException
    {
        try (Writer w = new OutputStreamWriter(out, "UTF-8"))
        {
            gson.toJson( data, w );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)