Reputation: 962
I'm creating a Service where I'm validating a field. If the field is not ok with my rules, I throw a ResourceBusinessException, which is a class that I've created to deal with Unprocessable entities. However, when this exception is thrown, Spring throws a NullPointerException on the class AbstractMessageConverterMethodProcessor.
To make it more readable of what I'm talking about, below I will attach all code that is related with this problem. To make it more simple, I have forced the exception on the code.
First of all, my spring version:
<spring-framework-version>4.2.6.RELEASE</spring-framework-version>
My Controller method:
/**
* @param MyType
* @return
*/
@ApiOperation(value = "", notes = "", authorizations = { @Authorization(value = ApplicationSwaggerConfig.securityOAuth2, scopes = { @AuthorizationScope(scope = WRITE_SCOPE, description = WRITE_DESCRIPTION) }) })
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 201, message = "Created"), @ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 422, message = "Unprocessable Entity") })
@RequestMapping(value = "/xpto", method = POST, produces = { APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE })
public ResponseEntity<Void> saveXPTO(@RequestBody MyType myType) {
Long tenantId = 1L;
MyClass myObject = service.findMyClass(tenantId, empresaType.getAttribute());
MyOtherClass myOtherObject= this.applyForOne(myObject);
PreconditionsRest.checkCondition(false, "ERROR!!"); // This is where I'm forcing an error.
contribuinteService.saveMyOtherObject(myOtherObject);
return new ResponseEntity<Void>(new HttpHeaders(), CREATED);
}
My service method:
/**
* Check condition.
*
* @param condition O(a)(s) condition
*/
public static void checkCondition(boolean condition, String message){
if (!condition) {
throw new ResourceBusinessException(message);
}
}
My ResourceBusinessException class:
public class ResourceBusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
private ErrorMessage errorMessage;
public ResourceBusinessException() {
super();
}
public ResourceBusinessException(String message, Throwable cause,boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public ResourceBusinessException(String message, Throwable cause) {
super(message, cause);
}
public ResourceBusinessException(String message) {
super(message);
}
public ResourceBusinessException(Throwable cause) {
super(cause);
}
public ResourceBusinessException(ErrorMessage errorMessage){
super(errorMessage.getDetail());
this.errorMessage = errorMessage;
}
public ErrorMessage getErrorMessage() {
return errorMessage;
}
}
My ResourceBusinessExceptionHandler class :
public class ResourceBusinessExceptionHandler extends ErrorMessageRestExceptionHandler<ResourceBusinessException>{
private static final Logger LOGGER = LoggerFactory.getLogger(ResourceBusinessExceptionHandler.class);
private ErrorMessage errorMessage;
public ResourceBusinessExceptionHandler() {
super(UNPROCESSABLE_ENTITY);
}
@Override
public ErrorMessage createBody(ResourceBusinessException ex, HttpServletRequest req) {
LOGGER.debug("ResourceBusinessExceptionHandler - Started");
ErrorMessage tmpl = super.createBody(ex, req);
ValidationErrorMessage msg = new ValidationErrorMessage(tmpl);
errorMessage=ex.getErrorMessage();
if(errorMessage==null){
errorMessage = new ErrorMessage();
}
msg.setType(ResourceHandlerUtil.getType(errorMessage,UNPROCESSABLE_ENTITY_SPEC));
msg.setTitle(ResourceHandlerUtil.getTitle(errorMessage,HttpStatus.UNPROCESSABLE_ENTITY));
msg.setDetail(ResourceHandlerUtil.getDetail(errorMessage,HttpStatus.UNPROCESSABLE_ENTITY));
msg.setInstance(ResourceHandlerUtil.getUriInstance(errorMessage,UNPROCESSABLE_ENTITY_URI));
LOGGER.debug("ResourceBusinessExceptionHandler - Complete");
return msg;
}
My configuration bean:
<bean id="compositeExceptionResolver" class="org.springframework.web.servlet.handler.HandlerExceptionResolverComposite">
<property name="order" value="0" />
<property name="exceptionResolvers">
<list>
<ref bean="exceptionHandlerExceptionResolver" />
<ref bean="restExceptionResolver" />
</list>
</property>
</bean>
<bean id="restExceptionResolver" class="cz.jirutka.spring.exhandler.RestHandlerExceptionResolverFactoryBean">
<property name="messageSource" ref="httpErrorMessageSource" />
<property name="defaultContentType" value="application/json" />
<property name="exceptionHandlers">
<map>
<!-- 422 -->
<entry key="com.company.project.exception.ResourceBusinessException">
<bean class="com.company.project.exception.handler.ResourceBusinessExceptionHandler"/>
</entry>
</map>
</property>
</bean><bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"/>
<bean id="httpErrorMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:defaultEncoding="UTF-8"/>
Then, when I make a request to my service, the following stack trace appears on my console :
15:26:09.119 [http-nio-8013-exec-3] DEBUG c.j.s.e.h.RestExceptionHandler - POST /api/xpto ~> 422 com.company.project.exception.ResourceBusinessException: ERROR!!
[...] And then, the NullPointerException...
ERROR c.j.s.e.RestHandlerExceptionResolver - Failed to process error response: <422 Unprocessable Entity,ValidationErrorMessage(errors=[]),{}> java.lang.NullPointerException: null at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.getGenericType(AbstractMessageConverterMethodProcessor.java:271) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:172) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at cz.jirutka.spring.exhandler.RestHandlerExceptionResolver.processResponse(RestHandlerExceptionResolver.java:141) ~[spring-rest-exception-handler-1.0.3.jar:1.0.3] at cz.jirutka.spring.exhandler.RestHandlerExceptionResolver.doResolveException(RestHandlerExceptionResolver.java:107) ~[spring-rest-exception-handler-1.0.3.jar:1.0.3] at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:137) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1185) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1022) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [servlet-api.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.36] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [catalina.jar:8.0.36] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.36] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.36] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) [catalina.jar:8.0.36] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) [tomcat-coyote.jar:8.0.36] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) [tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) [tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) [tomcat-coyote.jar:8.0.36] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_65] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_65] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65]
UPDATE
Below follows the method processResponse of the class RestHanlderExceptionResolver:
protected void processResponse(ResponseEntity<?> entity, NativeWebRequest webRequest) throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
try {
responseProcessor.handleReturnValue(entity, null, mavContainer, webRequest);
} catch (HttpMediaTypeNotAcceptableException ex) {
LOG.debug("Requested media type is not supported, falling back to default one");
fallbackResponseProcessor.handleReturnValue(entity, null, mavContainer, webRequest);
}
}
Upvotes: 0
Views: 2185
Reputation: 962
I found the problem. The version that I'm using of Spring is not compatible with the spring-rest-exception-handler-version. The rest exception handler that I'm using is from this github project: https://github.com/jirutka/spring-rest-exception-handler.
So if you are using the spring 4.2.6 RELEASE version, you should update the version of spring-rest-exception-handler to 1.2.0. This should solve the NullPointerException.
Upvotes: 0