Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. @Path("/A")
  2. public class ServiceA {
  3.  
  4. @GET
  5. @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  6. public Response show() {
  7. return Response.ok(new User("John", "Doe")).build();
  8. }
  9. }
  10.  
  11. @XmlRootElement
  12. public class User {
  13.  
  14. private String firstName;
  15. private String lastName;
  16.  
  17. public User() {
  18. }
  19.  
  20. public User(String firstName, String lastName) {
  21. this.firstName = firstName;
  22. this.lastName = lastName;
  23. }
  24.  
  25. public String getFirstName() {
  26. return firstName;
  27. }
  28.  
  29. public void setFirstName(String firstName) {
  30. this.firstName = firstName;
  31. }
  32.  
  33. public String getLastName() {
  34. return lastName;
  35. }
  36.  
  37. public void setLastName(String lastName) {
  38. this.lastName = lastName;
  39. }
  40. }
  41.  
  42. @XmlRootElement
  43. public class ErrorResponse {
  44.  
  45. private String errorType;
  46. private String errorMessage;
  47.  
  48. public ErrorResponse() {
  49. }
  50.  
  51. public ErrorResponse(String errorType, String errorMessage) {
  52. this.errorType = errorType;
  53. this.errorMessage = errorMessage;
  54. }
  55.  
  56. public String getErrorType() {
  57. return errorType;
  58. }
  59.  
  60. public void setErrorType(String errorType) {
  61. this.errorType = errorType;
  62. }
  63.  
  64. public String getErrorMessage() {
  65. return errorMessage;
  66. }
  67.  
  68. public void setErrorMessage(String errorMessage) {
  69. this.errorMessage = errorMessage;
  70. }
  71. }
  72.  
  73. @Provider
  74. public class GenericExceptionMapper implements ExceptionMapper<Exception> {
  75.  
  76. private final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class);
  77.  
  78. @Override
  79. public Response toResponse(Exception exception) {
  80. ErrorResponse errorResponse = new ErrorResponse(exception.getClass().getSimpleName(), exception.getMessage());
  81.  
  82. if (exception instanceof WebApplicationException) {
  83. LOG.error("Type: {}", exception.getClass().getSimpleName());
  84. LOG.error("Message: {}", exception.getMessage());
  85. WebApplicationException webApplicationException = (WebApplicationException) exception;
  86. return Response.status(webApplicationException.getResponse().getStatus()).entity(errorResponse).build();
  87. }
  88.  
  89. return Response.serverError().entity(errorResponse).build();
  90. }
  91. }
  92.  
  93. Accept: application/json
  94. GET http://localhost:8080/exception-mapper-example/rest/A
  95.  
  96. {"firstName":"John","lastName":"Doe"}
  97.  
  98. 2017-01-13 16:53:46,859 ERROR [com.aizaz.samples.exceptionmapper.GenericExceptionMapper] (default task-35) Type: NotAllowedException
  99. 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
  100. 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
  101. at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:180)
  102. at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:199)
  103. at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
  104. at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
  105. at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
  106. at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  107. at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
  108. at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  109. at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  110. at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
  111. at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  112. at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
  113. at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  114. at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  115. at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  116. at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  117. at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
  118. at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
  119. at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
  120. at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
  121. at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  122. at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
  123. at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  124. at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  125. at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
  126. at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
  127. at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
  128. at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
  129. at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
  130. at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
  131. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  132. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  133. at java.lang.Thread.run(Thread.java:745)
  134. 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
  135. at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:66)
  136. at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:176)
  137. ... 32 more
  138.  
  139. Caused by: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:
  140. Could not find MessageBodyWriter for response object of type:
  141. com.aizaz.samples.model.ErrorResponse of media type: application/octet-stream
  142.  
  143. Response.ok().type(MediaType.APPLICATION_JSON).build();
  144.  
  145. @Provider
  146. public class GenericExceptionMapper implements ExceptionMapper<Exception> {
  147.  
  148. @Context
  149. private HttpHeaders m_headers;
  150.  
  151. private final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class);
  152.  
  153. @Override
  154. public Response toResponse(Exception exception) {
  155. ErrorResponse errorResponse = new ErrorResponse(exception.getClass().getSimpleName(), exception.getMessage());
  156.  
  157. if (exception instanceof WebApplicationException) {
  158. LOG.error("Type: {}", exception.getClass().getSimpleName());
  159. LOG.error("Message: {}", exception.getMessage());
  160. WebApplicationException webApplicationException = (WebApplicationException) exception;
  161. return Response.status(webApplicationException.getResponse().getStatus()).entity(errorResponse).build();
  162. }
  163.  
  164. return Response.serverError().entity(errorResponse).type(m_headers.getMediaType()).build();
  165. }
  166. }
  167.  
  168. public class MyCustomException extends WebApplicationException
  169.  
  170. resteasy-jaxrs:3.1.0.Final
  171. class: ExceptionHandler
  172. method: public Response handleException(HttpRequest request, Throwable e)
  173.  
  174. // First try and handle it with a mapper
  175. if ((jaxrsResponse = executeExceptionMapper(e)) != null) {
  176. return jaxrsResponse;
  177. }
  178.  
  179. if (exception instanceof WebApplicationException) {
  180. WebApplicationException webApplicationException = (WebApplicationException) exception;
  181. return webApplicationException.getResponse();
  182. }
  183.  
  184. resteasy-jaxrs:3.1.0.Final
  185. class: SegmentNode
  186. method: public Match match(List<Match> matches, String httpMethod, HttpRequest request)
  187.  
  188. request.setAttribute(RESTEASY_CHOSEN_ACCEPT, sortEntry.getAcceptType());
  189. return sortEntry.match;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement