Passing JSON to WebService

I developed a web service in Java using the dropwizard shell. I want it to consume json.

My service code is

- resource class

@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 is a simple bean class.

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

Client code -

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

I get an error -

ClientHandlerException

Is there something wrong with this code?

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

Can someone please tell me what I can lose?

CUSTOMERS -

enter image description here

+7
java json web-services dropwizard
source share
3 answers

Set the type correctly and make the request correctly.

The problem is that you have nothing to deal with the answer.

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

... basically says that you are returning something that cannot be read, formatted, and came up with something useful.

You are returning the product type in your service, which is your octet stream, but I do not see where you have MessageBodyWriter to output this response in JSON.

You need:

  @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
source share

It seems that JSONObject cannot be serialized because no message writer can be found. Why don't you just enter InputBean as input?

Change the client code to:

 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 a constructor ClientResponse response = webResource.type(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .post(ClientResponse.class, data); return response.getEntity(String.class); } 
0
source share

Dropwizard prefers Jackson for serializing and deserializing JSON, in which case you should be able to pass an InputBean directly, you can also specify the mime type manually or use an Entity wrapper, for example.

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

For more information on creating a customized Jersey client, see http://www.dropwizard.io/1.2.2/docs/manual/client.html#jersey-client .

0
source share

All Articles