2016-07-31 81 views
0

我的服務器日誌滿了以下錯誤消息 無法找到在哪裏可以找到此問題,我有基於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 
+0

您試圖將字符串轉換成整數,但字符串爲空。並且您試圖將空值轉換爲整數,因此獲取NumberFormatException –

+0

檢查來自前端的控制器中輸入的參數是否正確。來自前端的值在該控制器上爲null,並且由於在控制器上定義的參數類型是整數,Spring會嘗試將整數空值轉換。這就是爲什麼你得到這個異常 –

+0

有很多的API,有沒有我啓用的東西,它告訴哪個控制器失敗? –

回答

0

在這裏,我給由您記錄錯誤和異常最好的程度,你輕鬆地調試錯誤的方式,這幫助。

1.登錄請求URL(控制器URL)和請求參數 - 日誌與其參數請求URL之前將其分配給controller.Means登錄之前它轉到控制器處理程序方法。 (這個樂於助人的你case.Because春天拋出異常之前,由處理器處理時嘗試請求參數對應控制器處理方法參數轉換)。通過這個,你知道什麼是參數是recive對於哪個控制器URL

2 。記錄請求URL和響應參數再次將請求url與其響應參數一起記錄。這有助於確定爲此URL發送的響應

3.用請求URL記錄異常 - 如果您正在生成自定義異常(如CustomException)。然後在採用url的CustomException類中創建一個字段,因此當您記錄它時,您可以輕鬆地確定創建該異常的URL。對於其他異常和錯誤,嘗試製作通用異常處理程序類,並在該類中調用此方法,該方法在catch方法中調用,捕獲Throwable並在該方法中記錄打印堆棧跟蹤

下面是示例代碼: -

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

共同處理方法

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


     } 

對於可以使用的ExceptionHandler春天,你可以很容易地找到關於谷歌搜索通過

春天的第一和第二點可以使用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 { 

} 

}