What would be the @DELETE methods of a RESTfull java api?

0

I am using FullEntityRepository of deltaspike , Response has to be a status 204 if it works, and status 400 if it does not. Any tips? Layer service:

@Transactional
public void deletar(Integer id){
  MotivoConcessao motivoConcessaoParaDeletar = repositorio.findBy(id);
  repositorio.remove(motivoConcessaoParaDeletar);
}

Camada resource:
@DELETE
@Path("{id}")
public Response deletar(@PathParam("id") @Min(value = 1, message = "{recursomotivoconcessao.id.min}") Integer id) {
  return Response.status(204).entity(servico.deletar(id)).build();
}
    
asked by anonymous 09.12.2017 / 16:06

1 answer

0

According to documentation of DeltaSpike, the remove method evokes the remove method of the EntityManager class. Because the last method throws a IllegalArgumentException if it fails, you could try to catch this exception and, if it does, send the response with the desired status. Example:

@DELETE
@Path("{id}")
public Response deletar(@PathParam("id") 
                        @Min(value = 1, message = "{ recursomotivoconcessao.id.min}") 
                        Integer id) {
    try {
        servico.deletar(id);
    } catch (IllegalArgumentException e) {
         return Response.status(409).build();
    }
    return Response.status(204).build();
}
    
09.12.2017 / 18:12