I am using Gson library to handle parsing Json to java entities and vice-versa. Now, after processing the in need to return the Json object to the caller. But doing so results in following exception:
Mar 25, 2014 4:46:30 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.google.gson.JsonObject, and Java type class com.google.gson.JsonObject, and MIME media type application/json was not found
Mar 25, 2014 4:46:30 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
Json after processing is {"status":"Entity added successfully."}
My Observation: It seems like i need to register Gson
implementation for Json somewhere to let the container know that I will send Json data using Gson's JsonObject. If i observed correctly, then where should i register and how, or if i am totally wrong then, please correct me.
My implementation looks like following:
@POST
@Path("/entity")
@Produces(MediaType.APPLICATION_JSON)
public Response addEntity(@FormParam("entity") String jsonEntity, @FormParam("entityType") String jsonEntityType) {
JsonObject jSonStatus = null;
log.info("Entered webservice method: " + jsonEntity.toString() + "\n" + jsonEntityType.toString());
if (jsonEntity != null && jsonEntityType != null) {
dispatcher = dispatcher.getDispatcher();
jSonStatus = dispatcher.addEntity(jsonEntity, jsonEntityType);
}
log.info("Returning from webservice method: " + jSonStatus);
ResponseBuilder rb = new ResponseBuilderImpl();
// rb.type(MediaType.APPLICATION_JSON); tried with this also, but no luck
rb.entity(jSonStatus);
rb.status(Status.OK);
return rb.build();
}