...
| Code Block |
|---|
package org.tynamo.examples.simple.rest;
import org.tynamo.examples.simple.MyDomainObject;
import org.tynamo.services.PersistenceService;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.List;
@Path("/mydomainobject")
public class MyDomainObjectResource
{
private PersistenceService persistenceService;
public MyDomainObjectResource(PersistenceService persistenceService) {
this.persistenceService = persistenceService;
}
@GET
@Produces({"application/xml", "application/json"})
public List<MyDomainObject> getAllDomains()
{
return (persistenceService.getInstances(MyDomainObject.class));
}
@POST
@Consumes@Produces({"application/xml", "application/json"})
public Response post(MyDomainObject domainObject)
{
persistenceService.save(domainObject);
return Response.ok().build();
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public MyDomainObject getDomainObject(@PathParam("id") Long id)
{
MyDomainObject domainObject = persistenceService.getInstance(MyDomainObject.class, id);
if (domainObject == null)
{
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return domainObject;
}
}
|
...
The previous examples use a the JSON marshaller/unmarshaller provided by the resteasy-jettison-provider library. This provider allows you to marshall JAXB annotated POJOs to and from JSON, it wraps the Jettison JSON library to accomplish this.
| Code Block |
|---|
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.0.1.GA</version>
</dependency> |
If you don't need JAXB and only you only need JSON support, you can use the JSON marshaller/unmarshaller provided by the resteasy-jackson-provider library. I personally find Jackson's output format more intuitive than the format provided by either BadgerFish or Jettison.
| Code Block |
|---|
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>2.3.0.GA</version> </dependency> |
You could use JAXB and Jackson together but be aware of the possible conflicts with the JAXB providers
Contribute singleton resources
...
