Reputation: 424
Hi I'm trying to return Json from a method in a simple rest using Jersey
HelloWorldService.class
@Path("/hello")
public class HelloWorldService {
@GET
@Path("/empdetail/{id}")
@Produces(MediaType.APPLICATION_JSON)
public EmpDetailsVo getEmpDetails(@PathParam("id") String id){
EmpDetailsVo details = new EmpDetailsVo();
details.empId="1202";
details.empName="Akhi";
details.empAddress="123 Main St. Newark PA 19121";
return details;
}
}
EmpDetailsVo
class has getter and setters for empId
, name
and address
.
When I try to run this url:
http://localhost:8080/jerseyRest/rest/hello/empdetail/1202
I get Http status 500
.
And on console I see an error:
SEVERE: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
And
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
Can someone please help.
Upvotes: 2
Views: 8394
Reputation: 18022
You need to tell Jersey how to marshal and unmarshal objects from the EmpDetailsVo
type into JSON.
Check this tutorial for an example of how this can be done. Here's another example using a different approach. Investigate the use of the com.sun.jersey.api.json.POJOMappingFeature
parameter provided to your web application via web.xml, and that should get you there.
Upvotes: 3