无法找到错误的位置

时间:2016-07-31 08:44:48

标签: linux spring-mvc logback

我的服务器日志已满,并显示以下错误消息 不知道在哪里可以找到这个问题,我有基于Spring的MVC控制器。我检查了每个地方,但无法找到。 (Tomcat 8,AWS Linux)

org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "null"
        at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)
        at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)
        at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:603)
        at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:676)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NumberFormatException: For input string: "null"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.valueOf(Integer.java:766)
        at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:193)
        at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
        at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:450)
        at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:423)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
        at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
        ... 43 more

1 个答案:

答案 0 :(得分:0)

在这里,我给出了最佳记录错误和异常的方法,这有助于您轻松地调试错误。

<强> 1。记录请求URL(控制器URL)和请求参数 -       在将请求url分配给controller之前,使用其参数记录请求url。在转到控制器处理程序方法之前记录它。 (这在你的情况下很有用。因为在尝试转换对应于控制器处理程序方法参数的请求参数时处理程序处理它之前的spring throw异常)。通过这个你知道什么是recive哪个控制器url

2.记录请求网址和响应参数再次使用响应参数记录请求网址。这有助于识别为此URL发送的响应

第3。使用请求URL记录异常 - 如果要生成自定义异常(如CustomException)。然后在CustomException类中创建一个带有url的字段,因此当您记录它时,您可以轻松识别创建此异常的url。对于其他异常和错误,尝试创建公共异常处理程序类,并在catch类中调用此类中的方法,该方法捕获Throwable并在该方法中记录打印堆栈跟踪

以下是示例代码: -

   public CustomException extends Exception{
    String requestUrl;
    String message;
    }

Common Handler方法

     public static handleException(Throwable throwable, url){
      //Log print stack trace


      }

对于春天你可以使用ExceptionHandler,你可以通过googling轻松找到它

对于春天的第一点和第二点,您可以使用HandlerInterceptorAdapter,请了解相关内容。

下面显示示例代码

public class LogInteceptor extends HandlerInterceptorAdapter{

private static final Logger logger = LoggerFactory
    .getLogger(LogInteceptor.class);


//This method called before controller method handle the request
@Override
public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception {


//Log other things also 

//Logging the request url
logger.info("Request URL {} request param is {}" +  request.getRequestURL().toString(), request);
//if returned false, we need to make sure 'response' is sent
return true;
}

//This called before the model view send to view page for render
@Override
public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    //we can add attributes in the modelAndView and use that in the view page
}

//This called when response is going to sent
@Override
public void afterCompletion(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex)
        throws Exception {

}

 }