小编Har*_*hal的帖子

发生异常时不会调用ResponseEntityExceptionHandler

我是新来的春天.我正在使用spring webmvc开发REST api.对于错误处理我得到了这个链接http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-rest-spring-mvc-exceptions

我试图在我的项目中使用ResponseEntityExceptionHandler.但是每当我的控制器抛出异常时,它就永远不会到达这个ResponseEntityExceptionHandler.

以下是我的代码段

调节器

@Controller
@RequestMapping("/hello")
public class HelloController {  
    private static final Logger logger = Logger.getLogger(HelloController.class);
    @RequestMapping(value="/{name}", method=RequestMethod.GET)
    public @ResponseBody String greet(@PathVariable(value = "name")String name ) throws InvalidInputException, ResourceNotFoundException{
        logger.info("start greet() "+name );
        System.out.println("start greet() "+name);
        String message = null;
        if("".equalsIgnoreCase(name))
        {
            throw new InvalidInputException("Invalid Input");
        }
        List<String> names = new ArrayList<String>();
        names.add("Harshal");
        names.add("Smitesh");
        if(names.contains(name)){
            message = "Hello "+ name;
        }else{
            throw new ResourceNotFoundException("Requested Resource not found");
        }
        System.out.println("end greet");
        logger.info("end greet()");
        return message;
    }
} …
Run Code Online (Sandbox Code Playgroud)

error-handling spring-mvc spring-security spring-3

9
推荐指数
1
解决办法
1万
查看次数