Simple Rest to return Json using jersey - java

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.

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.

Related

REST : Reading post body in jersey

I am not able to read json data in post. So far I have been successful is reading string data in get method. Now i am working on POST where I am sending following JSON in body :
{"name": "myname","address":"myaddress"}
I am also sending content-type : application/json header information in the request.
I have downloaded jersey moxy jar and corresponding dependencies. My model class is as following :
#XmlRootElement
public class Mymodel{
#XMLElement
public String name;
#XMLElement
public String address;
}
following is the extract of the resource method
#POST
#Consumes(MediaType.Application_JSON)
public Resource mymethod(Mymodel mode){
System.out.println(mode.name);
}
Am i missing something ? Error I am getting is that media type is not supported(415 Unsupported Media Type).
I don't have anything related to json in the web.xml.
As i mentioned in my post above ,I am using jersey moxy jar which actually supports json application type in jersey. So my understanding is that as suggested adding "jersey-media-json-jackson" jar will not add any value, or I need both jackson and moxy jar ?

JAX-RS #consume("text/plain") does not get the String

I have method like,
#POST
#Produces(MediaType.APPLICATION_XML)
#Consumes("text/plain")
public File addFile(String filePath){
return fileService.addFile(filePath);
}
And I am using "Postman rest client" for testing the post request and simply type a path like c:\myFile.txt in the raw section
but no String is passed to method, but when I hard-code the path it works
Is the problem from #consume ?
Please see the answer below
#POST
#Produces(MediaType.APPLICATION_XML)
#Consumes(MediaType.TEXT_PLAIN)
public File addFile(String filePath){
return fileService.addFile(filePath);
}
And the header content type
as text/plain while sending the request to server from postman
for your reference please see the image below
As stated above, there is no annotation to fetch the body of the request with Jax-RS ; The original definition of the service is correct.
The problem probably comes from postman settings.
You should select Body > raw > Text (text/plain).

How to change the response content type for a Dropwizard REST service?

I took a look at the Dropwizard framework and I want to use it to package my existing REST service.
In the tutorial I noticed that the response content type is not set using a ResponseBuilder, can I set the reponse type like I would do for a regular REST service if it were not in a Dropwizard framework ?
The reason I want to set a dynamic response content type is because the webservice does not know the kind of data it is serving.
Thanks
You should be able to just return a Response object and adjust the type. For instance:
#Path("/hello")
#Produces(MediaType.TEXT_PLAIN)
public class Example{
#GET
public Response sayHello() {
return Response.ok("Hello world").type(MediaType.TEXT_HTML).build();
}
}

Consuming JSON request and producing different output

Can you point me to article or explain me how to declare RESTful web service which consumes JSON request and based on parameter inside JSON produces output in different formats, meaning customer can get output in JSON but in pdf also. I'm using Java and RestEasy on JBoss 5.1.
You could map the request on a method returning a RestEasy Response object, using a ResponseBuilder to build your response, setting dynamically the mime type of the response depending on a parameter in your JSON.
#POST
#Path("/foo")
#Consumes("application/json")
public Response fooService(MyObject obj) {
MyResponseEntity entity = MyObjectService.retrieveSomethingFrom(obj);
return Response.status(200).entity(entity).type(obj.isXml() ? "text/xml" : "application/json").build();
}
This way if your MyObject domain object that represent incoming JSON has a parameter xml set to true, then the Response object is parameterized to produce text/xml otherwise it produces application/json. RestEasy should do the rest.
You can use this way
#Path("/")
public class Test {
#Path("/test")
#POST
#Consumes("application/json")
#Produces("text/plain")
public Response addOrderJSON(OrderDetails details) {...}
}

java restful service client accepting list of response

I am trying to write a java client for restful resource. The response for my request is a list of objects. I have the following code for the request. BUt i get some unmarshall exception. Could anyone let me know how to solve this ?
GenericType<List<Response>> genType = new GenericType<List<Response>>() {};
GenericType<List<Response>> response = (GenericType<List<Response>>)resource.path(paramPath).accept(MediaType.APPLICATION_JSON).get(genType);
my resource has the following code
#GET
#Path("/app/{Id}")
#Produces(MediaType.APPLICATION_JSON)
public List<Response> getAllKeyValuesByAppId(#PathParam("Id") Long Id){
...
...
}
Can you test you REST method without unmarshaling?
Are you sure that the message body contains exactly what you expect?

Categories

Resources