ControllerAdvice 中的错误管理导致 URI 和 404 重复
Error managment in ControllerAdvice lead to duplication in URI and 404
我有一个简单的休息控制器:
@RestController
@RequestMapping("/api/v1/")
public class OrderController {
@RequestMapping(value = "/orders2", method = RequestMethod.POST)
public OrderDto createOrder2(@RequestBody OrderDto order) throws Exception {
throw new Exception("Bouh!");
}
}
而且我想在全球范围内管理异常。根据我的阅读,它可以通过类似的方式完成:
@ControllerAdvice
public class ErrorController {
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ErrorDto handleConflict(HttpServletRequest request, Exception e) throws Exception {
ErrorDto o = new ErrorDto ();
o.setMessage(e.getMessage());
return o;
}
}
但是当我根据我的请求创建 post 时,出现以下错误:
26/10/2016 17:26:08.187 [http-nio-8080-exec-12] WARN o.s.web.servlet.PageNotFound -
No mapping found for HTTP request with URI [/duorest/api/v1/api/v1/orders2]
in DispatcherServlet with name 'rest'
不知道为什么uri会变成/duorest/api/v1/api/v1/orders2
一些事实:
- 我检查了调试,我的代码被执行了
- 如果我在 rest 控制器中移动该方法,我不会收到任何错误以及我所期望的(我的 ErrorDto 对象)
- Spring 框架版本 4.3.3.RELEASE
- Spring-data-rest-webmvc 版本 2.5.4.RELEASE
有人遇到过这个问题吗?或者任何提示?
解决了吗?如果不是,请尝试使用 handleConflict
方法中缺少 @ResponseBody
来执行。
我有一个简单的休息控制器:
@RestController
@RequestMapping("/api/v1/")
public class OrderController {
@RequestMapping(value = "/orders2", method = RequestMethod.POST)
public OrderDto createOrder2(@RequestBody OrderDto order) throws Exception {
throw new Exception("Bouh!");
}
}
而且我想在全球范围内管理异常。根据我的阅读,它可以通过类似的方式完成:
@ControllerAdvice
public class ErrorController {
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ErrorDto handleConflict(HttpServletRequest request, Exception e) throws Exception {
ErrorDto o = new ErrorDto ();
o.setMessage(e.getMessage());
return o;
}
}
但是当我根据我的请求创建 post 时,出现以下错误:
26/10/2016 17:26:08.187 [http-nio-8080-exec-12] WARN o.s.web.servlet.PageNotFound -
No mapping found for HTTP request with URI [/duorest/api/v1/api/v1/orders2]
in DispatcherServlet with name 'rest'
不知道为什么uri会变成/duorest/api/v1/api/v1/orders2
一些事实:
- 我检查了调试,我的代码被执行了
- 如果我在 rest 控制器中移动该方法,我不会收到任何错误以及我所期望的(我的 ErrorDto 对象)
- Spring 框架版本 4.3.3.RELEASE
- Spring-data-rest-webmvc 版本 2.5.4.RELEASE
有人遇到过这个问题吗?或者任何提示?
解决了吗?如果不是,请尝试使用 handleConflict
方法中缺少 @ResponseBody
来执行。