Java Spring@ExceptionHandler不处理错误

Java Spring@ExceptionHandler不处理错误,java,spring,error-handling,converters,Java,Spring,Error Handling,Converters,我有一门课 @RestController public class CustomerRestController { @Autowired private CustomerManagementService customerManagementService; /** * * @return */ @ExceptionHandler(CustomerNotFoundException.class) public Resp

我有一门课

@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);
    }

}
调度程序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>
聚甲醛


4.0.0
com.mycompany
CRM项目
1.0-快照
战争
UTF-8
1.8
1.8
CRM项目
org.springframework
弹簧芯
4.3.6.1发布
org.springframework
spring上下文
4.3.5.1发布
org.springframework
春豆
4.3.5.1发布
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.0.1草案-16
org.springframework
spring上下文支持
4.3.5.1发布
org.apache.derby
德比
10.13.1.1
org.apache.derby
德比客户机
10.13.1.1
org.springframework
春季甲虫
4.3.5.1发布
org.eclipse.persistence
javax.persistence
2.1.1
公共dbcp
公共dbcp
1.4
org.aspectj
aspectjrt
1.8.10
org.aspectj
aspectjtools
1.8.10
org.hibernate
休眠实体管理器
4.3.6.最终版本
org.hibernate
冬眠核心
4.3.6.最终版本
org.springframework
弹簧网
4.3.6.1发布
org.springframework
春季方面
4.3.5.1发布
org.springframework
弹簧网袋
4.3.6.1发布
org.springframework
德克萨斯州春季
4.3.6.1发布
org.springframework
spring消息
4.3.5.1发布
org.springframework
弹簧oxm
4.3.5.1发布
org.springframework
弹簧乐器
4.3.5.1发布
奥帕林
奥帕林
1
org.springframework
SpringWebMVC
4.3.6.1发布

@ResponseBody
添加到异常处理程序方法中。使用
@RestController
时,默认情况下只有
@RequestMapping
方法使用
@ResponseBody


也许您注册了另一个异常解析程序。更改异常句柄异常解析器的优先级

如果使用注释配置:

@Bean
public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver()         
{
  ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
  exceptionHandlerExceptionResolver.setOrder(1);
  return exceptionHandlerExceptionResolver;
}
<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    <property name="order" value="1"/>
</bean>
如果使用xml配置:

@Bean
public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver()         
{
  ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
  exceptionHandlerExceptionResolver.setOrder(1);
  return exceptionHandlerExceptionResolver;
}
<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    <property name="order" value="1"/>
</bean>


您确定正在引发异常吗?看起来服务返回null,这就是您看到的

添加了ResponseBook注释,但没有任何区别。是的,我是:严重的:com.mycompany.crmproject.services.customer.CustomerNotFoundException位于。。。com.mycompany.crmproject.restcontroller.CustomerRestController.requestCustomerById(CustomerRestController.java:44)这只是日志消息,可能是异常被记录并吞没了吗?异常由aop代理处理,Thx for helpYou配置看起来不错,我无法在Tomcat中重现您的问题。你能展示一下你的pom吗?你能尝试使用不同的网络容器吗?@artemisian我已经上传了pom
<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    <property name="order" value="1"/>
</bean>