Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Path("/A")
- public class ServiceA {
- @GET
- @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
- public Response show() {
- return Response.ok(new User("John", "Doe")).build();
- }
- }
- @XmlRootElement
- public class User {
- private String firstName;
- private String lastName;
- public User() {
- }
- public User(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- }
- @XmlRootElement
- public class ErrorResponse {
- private String errorType;
- private String errorMessage;
- public ErrorResponse() {
- }
- public ErrorResponse(String errorType, String errorMessage) {
- this.errorType = errorType;
- this.errorMessage = errorMessage;
- }
- public String getErrorType() {
- return errorType;
- }
- public void setErrorType(String errorType) {
- this.errorType = errorType;
- }
- public String getErrorMessage() {
- return errorMessage;
- }
- public void setErrorMessage(String errorMessage) {
- this.errorMessage = errorMessage;
- }
- }
- @Provider
- public class GenericExceptionMapper implements ExceptionMapper<Exception> {
- private final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class);
- @Override
- public Response toResponse(Exception exception) {
- ErrorResponse errorResponse = new ErrorResponse(exception.getClass().getSimpleName(), exception.getMessage());
- if (exception instanceof WebApplicationException) {
- LOG.error("Type: {}", exception.getClass().getSimpleName());
- LOG.error("Message: {}", exception.getMessage());
- WebApplicationException webApplicationException = (WebApplicationException) exception;
- return Response.status(webApplicationException.getResponse().getStatus()).entity(errorResponse).build();
- }
- return Response.serverError().entity(errorResponse).build();
- }
- }
- Accept: application/json
- GET http://localhost:8080/exception-mapper-example/rest/A
- {"firstName":"John","lastName":"Doe"}
- 2017-01-13 16:53:46,859 ERROR [com.aizaz.samples.exceptionmapper.GenericExceptionMapper] (default task-35) Type: NotAllowedException
- 2017-01-13 16:53:46,860 ERROR [com.aizaz.samples.exceptionmapper.GenericExceptionMapper] (default task-35) Message: RESTEASY003650: No resource method found for POST, return 405 with Allow header
- 2017-01-13 16:53:46,860 ERROR [io.undertow.request] (default task-35) UT005023: Exception handling request to /exception-mapper-example/rest/A: org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.aizaz.samples.model.ErrorResponse of media type: application/octet-stream
- at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:180)
- at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:199)
- at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
- at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
- at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
- at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
- at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
- at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
- at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
- at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
- at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
- at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
- at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
- at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
- at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
- at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
- at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
- at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
- at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
- at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
- at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
- at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
- at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
- at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
- at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
- at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
- at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
- at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
- at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
- at java.lang.Thread.run(Thread.java:745)
- Caused by: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.aizaz.samples.model.ErrorResponse of media type: application/octet-stream
- at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:66)
- at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:176)
- ... 32 more
- Caused by: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:
- Could not find MessageBodyWriter for response object of type:
- com.aizaz.samples.model.ErrorResponse of media type: application/octet-stream
- Response.ok().type(MediaType.APPLICATION_JSON).build();
- @Provider
- public class GenericExceptionMapper implements ExceptionMapper<Exception> {
- @Context
- private HttpHeaders m_headers;
- private final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class);
- @Override
- public Response toResponse(Exception exception) {
- ErrorResponse errorResponse = new ErrorResponse(exception.getClass().getSimpleName(), exception.getMessage());
- if (exception instanceof WebApplicationException) {
- LOG.error("Type: {}", exception.getClass().getSimpleName());
- LOG.error("Message: {}", exception.getMessage());
- WebApplicationException webApplicationException = (WebApplicationException) exception;
- return Response.status(webApplicationException.getResponse().getStatus()).entity(errorResponse).build();
- }
- return Response.serverError().entity(errorResponse).type(m_headers.getMediaType()).build();
- }
- }
- public class MyCustomException extends WebApplicationException
- resteasy-jaxrs:3.1.0.Final
- class: ExceptionHandler
- method: public Response handleException(HttpRequest request, Throwable e)
- // First try and handle it with a mapper
- if ((jaxrsResponse = executeExceptionMapper(e)) != null) {
- return jaxrsResponse;
- }
- if (exception instanceof WebApplicationException) {
- WebApplicationException webApplicationException = (WebApplicationException) exception;
- return webApplicationException.getResponse();
- }
- resteasy-jaxrs:3.1.0.Final
- class: SegmentNode
- method: public Match match(List<Match> matches, String httpMethod, HttpRequest request)
- request.setAttribute(RESTEASY_CHOSEN_ACCEPT, sortEntry.getAcceptType());
- return sortEntry.match;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement