Skip to content

Instantly share code, notes, and snippets.

@EdoardoVignati
Last active May 7, 2019 09:58
Show Gist options
  • Save EdoardoVignati/b9ccda7cf3bcbdb774af7b0e1348490d to your computer and use it in GitHub Desktop.
Save EdoardoVignati/b9ccda7cf3bcbdb774af7b0e1348490d to your computer and use it in GitHub Desktop.
Jersey Exception handling
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response myMethod(Myobject myobject) throws MappingExceptionHandler{
/* code */
}
---------------------------------------------------------------------------------------------------
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/* import java.io.EOFException; */
@Provider
public class MappingExceptionHandler extends Exception implements ExceptionMapper<Exception>{
/*The ExceptionMapper<> can manage your Specific Exception (EOFException, MyException...)*/
private static final long serialVersionUID = 1L;
public MappingExceptionHandler() {
super("This is the error message!");
}
public MappingExceptionHandler(String string) {
super(string);
}
@Override
public Response toResponse(Exception exception)
{
return Response.status(404).entity(exception.getMessage())
.type("text/plain").build();
}
}
/* https://howtodoinjava.com/jersey/jaxrs-jersey-exceptionmapper/ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment