Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestEasy - Jax-rs - Sending custom Object in response body

How do I send my custom object in a response. I just want the values printed from my object.

Lets say I have an object of type Person. I am trying to send in REST response body like this.

  ResponseBuilder response = Response.ok().entity(personObj);
  return response.build();

But I get 500 error. Tried this one too:

  ResponseBuilder response = Response.status(Status.OK).entity(personObj);
  return response.build();

Same error.

Tried setting content type as text/xml. No use. What am I missing here? I tried googling. But not many examples out there, especially with the custom objects;

It returns fine, if I just pass a string to entity() method.

like image 839
Kevin Rave Avatar asked Jul 08 '13 18:07

Kevin Rave


People also ask

What is Jax-RS and RESTEasy?

So JAX-RS is a specification of how a library for implementing REST APIs in Java should look like and RESTEasy is one implementation of that specification. This effectively means that any documentation on JAX-RS should apply 1:1 to RESTEasy as well.

What is response OK () build ()?

ok(). build(); } // return 404 to allow load balancers to only send traffic to the coordinator return Response. status(Response.Status.NOT_FOUND).

How do you return a response in Java?

The Response class is an abstract class that contains three simple methods. The getEntity() method returns the Java object you want converted into an HTTP message body. The getStatus() method returns the HTTP response code. The getMetadata() method is a MultivaluedMap of response headers.


1 Answers

In order to return data from a Resteasy resource method you need to do several things depending on what you are trying to return.

  • You need to annotate your resource method with the @Produces annotation to tell Resteasy what the return type of the method should be.

    For example, the method below returns XML and JSON depending on what the client asks for in their Accept header.

@GET
@Produces({MediaType.APPLICATION_JSON, 
           MediaType.APPLICATION_XML})
public Response foo()
{
     PersonObj obj = new PersonObj();

     //Do something...
     return Response.ok().entity(obj).build();
}

Resteasy supports marshalling the following datatypes by default:

enter image description here

If the datatypes you wish to support are in this table then that means they are supported by JAXB and all you need to do is annotate your PersonObj class with JAXB annotations to tell it how to marshall and unmarshall the object.

@XmlRootElement
@XmlType(propOrder = {"firstName", "lastName"})
public class PersonObj
{
  private String firstName;
  private String lastName;

  //Getters and Setters Removed For Brevity
}

What if your content-type is not supported out of the box?

If you have a custom content-type that you would like to marshall then you need to create a MessageBodyWriter implementation that will tell Resteasy how to marshall the type.

Provider
@Produces({"application/x-mycustomtype"})
public class MyCustomTypeMessageBodyWriter implements MessageBodyWriter {

}

Just implement the interface and register it like any other Provider.

If you would like to read a custom content-type then you need to implement a custom MessageBodyReader to handle the incoming type and add it to the @Consumes annotation on your receiving method.

like image 156
gregwhitaker Avatar answered Oct 15 '22 20:10

gregwhitaker