Last active
May 7, 2019 09:58
-
-
Save EdoardoVignati/b9ccda7cf3bcbdb774af7b0e1348490d to your computer and use it in GitHub Desktop.
Jersey Exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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