将JSON传递给WebService

问题描述 投票:7回答:3

我使用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);
   }
} 

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

客户代码 -

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

我收到一个错误 -

ClientHandlerException

这段代码有什么问题吗?

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

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

客户日志 -

java json web-services dropwizard
3个回答
0
投票

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

问题是你无法处理响应。

A message body reader for Java class my.class.path.InputBean

...基本上是在说,你要归还的东西是无法读取,格式化和输入任何有用的东西。

您在服务中返回了一个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 );
        }
    }
}

0
投票

似乎JSONObject无法序列化,因为找不到消息编写器。为什么不直接将InputBean作为输入?

将您的客户代码更改为:

public String getProduct() {

        Client client = Client.create();
        WebResource webResource =       client.resource("http://localhost:8080/product/getProduct");
        InputBean data = new InputBean(1,1); // make sure there's a constructor
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, data);
        return response.getEntity(String.class);
    }

0
投票

Dropwizard优先使用Jackson进行JSON序列化和反序列化,在这种情况下,您应该能够直接传递InputBean,您也可以手动指定mime类型或使用Entity包装器,例如:

    final Client client = new JerseyClientBuilder(environment)
            .using(config.getJerseyClientConfiguration())
            .build("jersey-client");
    WebResource webResource = client.resource("http://localhost:8080/product/getProduct");
    InputBean data = new InputBean(1,1);
    String response = webResource.post(String.class, Entity.json(data));

有关如何创建配置的Jersey客户端的详细信息,请参阅http://www.dropwizard.io/1.2.2/docs/manual/client.html#jersey-client

© www.soinside.com 2019 - 2024. All rights reserved.