Returning 404 status code if entity is not found in web service method.

FEA: ItEr75S12AllowExportOneEntity
This commit is contained in:
Manuel Rego Casasnovas 2011-07-11 12:59:26 +02:00
parent b80613d8f0
commit 0188c7bc81
3 changed files with 31 additions and 7 deletions

View file

@ -21,6 +21,8 @@
package org.navalplanner.ws.calendars.api;
import javax.ws.rs.core.Response;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
@ -33,8 +35,9 @@ public interface ICalendarService {
BaseCalendarListDTO getBaseCalendars();
public InstanceConstraintViolationsListDTO addBaseCalendars(
InstanceConstraintViolationsListDTO addBaseCalendars(
BaseCalendarListDTO BaseCalendraListDTO);
public BaseCalendarDTO getBaseCalendar(String code);
Response getBaseCalendar(String code);
}

View file

@ -29,6 +29,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
import org.navalplanner.business.calendars.entities.BaseCalendar;
@ -104,7 +105,8 @@ public class CalendarServiceREST extends
@GET
@Path("/{code}/")
@Transactional(readOnly = true)
public BaseCalendarDTO getBaseCalendar(@PathParam("code") String code) {
return findByCode(code);
public Response getBaseCalendar(@PathParam("code") String code) {
return getDTOByCode(code);
}
}

View file

@ -24,6 +24,9 @@ package org.navalplanner.ws.common.impl;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.navalplanner.business.common.IAdHocTransactionService;
import org.navalplanner.business.common.IOnTransaction;
import org.navalplanner.business.common.IntegrationEntity;
@ -204,12 +207,28 @@ public abstract class GenericRESTService<E extends IntegrationEntity,
* @param code
* this is the code for the element which will be searched
* @return DTO which represents the IntegrationEntity with this code
* @throws InstanceNotFoundException
* If entity with this code is not found
*/
protected DTO findByCode(String code) {
protected DTO findByCode(String code) throws InstanceNotFoundException {
return toDTO(getIntegrationEntityDAO().findByCode(code));
}
/**
* Wraps within a {@link Response} object the DTO searching the entity by
* code.
*
* If entity is not found returns 404 HTTP status code (NOT_FOUND).
*
* @param code
* this is the code for the element which will be searched
* @return The {@link Response} with DTO if OK or 404 if NOT_FOUND
*/
protected Response getDTOByCode(String code) {
try {
return toDTO(getIntegrationEntityDAO().findByCode(code));
return Response.ok(findByCode(code)).build();
} catch (InstanceNotFoundException e) {
return null;
return Response.status(Status.NOT_FOUND).build();
}
}