[
https://issues.jboss.org/browse/FORGE-1273?page=com.atlassian.jira.plugin...
]
Antonio Goncalves commented on FORGE-1273:
------------------------------------------
Hum... the web is full of talks about this issue (eg.
http://stackoverflow.com/questions/2195639/restful-resource-not-found-404...).
I've attached two screenshots of Bill Burke's book about JAX-RS (I have it in
Kindle format so I couldn't copy/paste the code). When an entity is not found, or a
query returns nothing, JPA returns null : it's not an exception, it's a normal
case where no data is returned. A 404 is when the resource is not found (eg. you have a
typo in your curl and the GET method is not invoked). A 204 is saying that the resource
exists, you invoked it, everything is ok but it returned null (or no content)
REST NOT_FOUND should be NO_CONTENT
-----------------------------------
Key: FORGE-1273
URL:
https://issues.jboss.org/browse/FORGE-1273
Project: Forge
Issue Type: Enhancement
Components: Scaffold
Affects Versions: 1.4.2.Final
Reporter: Antonio Goncalves
Fix For: 2.x Future
Attachments: rest.tiff, rest2.tiff
Hi,
In the REST endpoint generated code we have the following methods that return a
Status.NOT_FOUND status in case the entity is not found :
{code}
@DELETE
@Path("/{id:[0-9][0-9]*}")
public Response deleteById(@PathParam("id") Long id)
{
Book entity = em.find(Book.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<Book> findByIdQuery = em.createQuery("SELECT DISTINCT b FROM
Book b WHERE b.id = :entityId ORDER BY b.id", Book.class);
findByIdQuery.setParameter("entityId", id);
Book entity;
try
{
entity = findByIdQuery.getSingleResult();
}
catch (NoResultException nre)
{
entity = null;
}
if (entity == null)
{
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(entity).build();
}
{code}
In both cases it should be a Status.NO_CONTENT. The resource exists (otherwise it would
have been impossible to invoke it) it just happens that it returns an empty body (with a
no content status)
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see:
http://www.atlassian.com/software/jira