Java – pass JSON to webservice
•
Java
I developed a web service in Java using the dropwizard framework I hope it consumes a JSON
My service code is –
– resource level
@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);
}
}
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;
}
}
Customer 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 received an error –
ClientHandlerException
Is there anything 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 anyone point out what I might be missing?
Customer log –
Solution
You set the type correctly and make the request correctly
The problem is that you can't handle the response
A message body reader for Java class my.class.path.InputBean
... basically, what you want to return is that you can't read, format and enter anything useful
You returned a product type in the service, which is your octet stream, but I don't see where you output messagebodywriter to 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,Annotation[] annotations,MediaType mediaType)
{
if ( mediaType.equals(MediaType.APPLICATION_JSON_TYPE) )
{
return Product.class.isAssignableFrom(type);
}
return false;
}
@Override
public void writeTo(Product data,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 );
}
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
