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.
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.
ok(). build(); } // return 404 to allow load balancers to only send traffic to the coordinator return Response. status(Response.Status.NOT_FOUND).
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.
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:
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 }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With