Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created April 19, 2015 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/f67b2c2474808e80a81b to your computer and use it in GitHub Desktop.
Save thjanssen/f67b2c2474808e80a81b to your computer and use it in GitHub Desktop.
JBoss Forge - Speedup your enterprise development - Part II RESTful Webservices (http://www.thoughts-on-java.org/2013/09/rapid-application-development-with.html)
@Entity
@XmlRootElement
public class Author implements Serializable
{
...
@XmlTransient
public Set<Book> getBooks()
{
return this.books;
}
...
}
@Stateless
@Path("/authors")
public class AuthorEndpoint
{
@PersistenceContext(unitName = "forge-default")
private EntityManager em;
@POST
@Consumes("application/xml")
public Response create(Author entity)
{
em.persist(entity);
return Response.created(UriBuilder.fromResource(AuthorEndpoint.class).path(String.valueOf(entity.getId())).build()).build();
}
@DELETE
@Path("/{id:[0-9][0-9]*}")
public Response deleteById(@PathParam("id") Long id)
{
Author entity = em.find(Author.class, id);
if (entity == null)
{
return Response.status(Status.NOT_FOUND).build();
}
em.remove(entity);
return Response.noContent().build();
}
@GET
@Path("/{id:[0-9][0-9]*}")
@Produces("application/xml")
public Response findById(@PathParam("id") Long id)
{
TypedQuery<Author> findByIdQuery = em.createQuery("SELECT a FROM Author a LEFT JOIN FETCH a.books WHERE a.id = :entityId", Author.class);
findByIdQuery.setParameter("entityId", id);
Author entity = findByIdQuery.getSingleResult();
if (entity == null)
{
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(entity).build();
}
@GET
@Produces("application/xml")
public List<Author> listAll()
{
final List<Author> results = em.createQuery("SELECT a FROM Author a LEFT JOIN FETCH a.books", Author.class).getResultList();
return results;
}
@PUT
@Path("/{id:[0-9][0-9]*}")
@Consumes("application/xml")
public Response update(@PathParam("id") Long id, Author entity)
{
entity.setId(id);
entity = em.merge(entity);
return Response.noContent().build();
}
}
@Stateless
@Path("/authors")
public class AuthorEndpoint
{
...
@GET
@Path("/{id:[0-9][0-9]*}")
@Produces("application/xml")
public Response findById(@PathParam("id") Long id)
{
TypedQuery<Author> findByIdQuery = em.createQuery("SELECT a FROM Author a WHERE a.id = :entityId", Author.class);
findByIdQuery.setParameter("entityId", id);
Author entity = findByIdQuery.getSingleResult();
if (entity == null)
{
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(entity).build();
}
@GET
@Produces("application/xml")
public List<Author> listAll()
{
final List<Author> results = em.createQuery("SELECT a FROM Author a", Author.class).getResultList();
return results;
}
...
}
[ForgeExample] Author.java $ rest endpoint-from-entity --contentType
application/xml blog.thoughts.on.java.forge.model.*;
***SUCCESS*** Generated REST endpoint for
[blog.thoughts.on.java.forge.model.Author]
***SUCCESS*** Generated REST endpoint for
[blog.thoughts.on.java.forge.model.Book]
Wrote D:\/dev/wrk4/forge/ForgeExample/src/main/java/blog/thoughts/on/java
/forge/model/Author.java
Wrote D:\/dev/wrk4/forge/ForgeExample/src/main/java/blog/thoughts/on/java
/forge/rest/AuthorEndpoint.java
Wrote D:\/dev/wrk4/forge/ForgeExample/src/main/java/blog/thoughts/on/java
/forge/model/Book.java
Wrote D:\/dev/wrk4/forge/ForgeExample/src/main/java/blog/thoughts/on/java
/forge/rest/BookEndpoint.java
[ForgeExample] Author.java $ rest setup --activatorType APP_CLASS;
? What root path do you want to use for your resources? [/rest]
? In what package do you want to store the Application class?
[blog.thoughts.on.java.forge.rest]
? How do you want to name the Application class? [RestApplication]
***SUCCESS*** Installed [forge.spec.jaxrs.applicationclass] successfully.
***SUCCESS*** Installed [forge.spec.jaxrs] successfully.
***SUCCESS*** Rest Web Services (JAX-RS) is installed.
Wrote D:\/dev/wrk4/forge/ForgeExample/src/main/java/blog/thoughts/on/
java/forge/rest/RestApplication.java
Wrote D:\/dev/wrk4/forge/ForgeExample/pom.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment