如何让Spring 3.0控制器触发404?
我有一个控制器@RequestMapping(值= "/**",方法= RequestMethod.GET)和一些访问控制器的url,我希望容器提出一个404。
如何让Spring 3.0控制器触发404?
我有一个控制器@RequestMapping(值= "/**",方法= RequestMethod.GET)和一些访问控制器的url,我希望容器提出一个404。
当前回答
虽然标记的答案是正确的,但有一种方法可以做到这一点,没有例外。服务返回搜索对象的Optional<T>,这被映射到HttpStatus。如果找到OK,如果为空则返回404。
@Controller
public class SomeController {
@RequestMapping.....
public ResponseEntity<Object> handleCall(@PathVariable String param) {
return service.find(param)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
@Service
public class Service{
public Optional<Object> find(String param){
if(!found()){
return Optional.empty();
}
...
return Optional.of(data);
}
}
其他回答
我建议抛出HttpClientErrorException,就像这样
@RequestMapping(value = "/sample/")
public void sample() {
if (somethingIsWrong()) {
throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
}
}
您必须记住,这只能在将任何内容写入servlet输出流之前进行。
使用setting配置web.xml
<error-page>
<error-code>500</error-code>
<location>/error/500</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/404</location>
</error-page>
创建新控制器
/**
* Error Controller. handles the calls for 404, 500 and 401 HTTP Status codes.
*/
@Controller
@RequestMapping(value = ErrorController.ERROR_URL, produces = MediaType.APPLICATION_XHTML_XML_VALUE)
public class ErrorController {
/**
* The constant ERROR_URL.
*/
public static final String ERROR_URL = "/error";
/**
* The constant TILE_ERROR.
*/
public static final String TILE_ERROR = "error.page";
/**
* Page Not Found.
*
* @return Home Page
*/
@RequestMapping(value = "/404", produces = MediaType.APPLICATION_XHTML_XML_VALUE)
public ModelAndView notFound() {
ModelAndView model = new ModelAndView(TILE_ERROR);
model.addObject("message", "The page you requested could not be found. This location may not be current.");
return model;
}
/**
* Error page.
*
* @return the model and view
*/
@RequestMapping(value = "/500", produces = MediaType.APPLICATION_XHTML_XML_VALUE)
public ModelAndView errorPage() {
ModelAndView model = new ModelAndView(TILE_ERROR);
model.addObject("message", "The page you requested could not be found. This location may not be current, due to the recent site redesign.");
return model;
}
}
虽然标记的答案是正确的,但有一种方法可以做到这一点,没有例外。服务返回搜索对象的Optional<T>,这被映射到HttpStatus。如果找到OK,如果为空则返回404。
@Controller
public class SomeController {
@RequestMapping.....
public ResponseEntity<Object> handleCall(@PathVariable String param) {
return service.find(param)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
@Service
public class Service{
public Optional<Object> find(String param){
if(!found()){
return Optional.empty();
}
...
return Optional.of(data);
}
}
如果你想从控制器返回404状态,你只需要这样做
@RequestMapping(value = "/something", method = RequestMethod.POST)
@ResponseBody
public HttpStatus doSomething(@RequestBody String employeeId) {
try {
return HttpStatus.OK;
}
catch (Exception ex) {
return HttpStatus.NOT_FOUND;
}
}
通过这样做,当您想从控制器返回404时,您将收到一个404错误。
重写你的方法签名,使它接受HttpServletResponse作为参数,这样你就可以对它调用setStatus(int)。
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-arguments