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 ); } } }
stark0788
source share