2017-03-02 47 views
0

我有一個像春@ExceptionHandler犯規處理錯誤

@RestController 
public class CustomerRestController { 

    @Autowired 
    private CustomerManagementService customerManagementService; 

    /** 
    * 
    * @return 
    */ 
    @ExceptionHandler(CustomerNotFoundException.class) 
    public ResponseEntity<ClinetErrorInformation> rulesForCustomerNotFound() { 

     ClinetErrorInformation clinetErrorInformation = new ClinetErrorInformation("Clinet was not found"); 
     return new ResponseEntity<>(clinetErrorInformation, HttpStatus.NOT_FOUND); 
    } 

    @RequestMapping(value = {"/customer/{id}"}) 
    public Customer requestCustomerById(@PathVariable String id) throws CustomerNotFoundException { 
     return customerManagementService.findCustomerById(id); 
    } 

} 

唯一的例外是由類沒有處理類:

嚴重:com.mycompany.crmproject.services.customer.CustomerNotFoundException 在 ... com.mycompany.crmproject.restcontroller.CustomerRestController.requestCustomerById(CustomerRestController.java:44)

調度的Servlet:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


    <context:component-scan base-package="com.mycompany.crmproject.restcontroller"/> 


    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/> 

    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
     <property name="favorPathExtension" value="false"/> 

     <property name="favorParameter" value="true"/> 

     <property name="parameterName" value="mediaType"/> 

     <property name="mediaTypes"> 
      <map> 
       <entry key="json" value="application/json"></entry> 
       <entry key="xml" value="application/xml"></entry>    
      </map> 
     </property> 

     <property name="ignoreAcceptHeader" value="false"/> 

    </bean> 

</beans> 

什麼可能是爲什麼這個錯誤沒有處理,並在browesr我只能看到空白頁。

HTTP響應

Status Code: 200 OK 
Content-Length: 0 
Date: Thu, 02 Mar 2017 14:09:52 GMT 
Server: GlassFish Server Open Source Edition 4.1.1 
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1.1 Java/Oracle Corporation/1.8) 

雙龍

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>CRMProject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
    <name>CRMProject</name> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.3.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.1-api</artifactId> 
      <version>1.0.0.Draft-16</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derby</artifactId> 
      <version>10.13.1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derbyclient</artifactId> 
      <version>10.13.1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>javax.persistence</artifactId> 
      <version>2.1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>1.8.10</version> 
     </dependency> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjtools</artifactId> 
      <version>1.8.10</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.6.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.6.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.3.6.RELEASE</version> 
     </dependency>   
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aspects</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-websocket</artifactId> 
      <version>4.3.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>4.3.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-messaging</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-oxm</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-instrument</artifactId> 
      <version>4.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>aopalliance</groupId> 
      <artifactId>aopalliance</artifactId> 
      <version>1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.3.6.RELEASE</version> 
     </dependency> 
    </dependencies> 
</project> 
+1

你的配置看起來不錯,我不能在Tomcat中重現你的問題。你能展示你的POM嗎?你可以嘗試使用不同的網絡容器? – artemisian

+0

@artemisian我上傳了pom – pshemek

回答

0

確定異常被拋出?看起來像服務返回null,這就是你看到的。

+0

是我: 嚴重:com.mycompany.crmproject.services.customer.CustomerNotFoundException在... com.mycompany.crmproject.restcontroller.CustomerRestController.requestCustomerById(CustomerRestController.java:44) – pshemek

+0

這不僅僅是日誌信息,這樣可以該異常是否被記錄和吞噬? –

+0

異常由aop代理處理,Thx尋求幫助 – pshemek

0

添加@ResponseBody到您的異常處理方法。使用@RestController默認情況下,只有@RequestMapping方法使用@ResponseBody


也許你有另一個異常解析器註冊。將優先級更改爲ExceptionHandlerExceptionResolver

如果使用註釋配置:

@Bean 
public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver()   
{ 
    ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver(); 
    exceptionHandlerExceptionResolver.setOrder(1); 
    return exceptionHandlerExceptionResolver; 
} 

如果您使用XML配置:

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"> 
    <property name="order" value="1"/> 
</bean> 
+0

增加了ResponseBody註釋,但它沒有什麼區別。 – pshemek

+0

@pshemek我已經更新了我的答案。 – alfcope