使用spring 3 restful以编程方式更改http响应状态 [英] Programmatically change http response status using spring 3 restful
本文介绍了使用spring 3 restful以编程方式更改http响应状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似下面的控制器
I have a controller like below
@Controller("myController")
@RequestMapping("api")
public class MyController {
@RequestMapping(method = RequestMethod.GET, value = "/get/info/{id}", headers = "Accept=application/json")
public @ResponseBody
Student getInfo(@PathVariable String info) {
.................
}
@ExceptionHandler(Throwable.class)
@ResponseStatus( HttpStatus.EXPECTATION_FAILED)
@ResponseBody
public String handleIOException(Throwable ex) {
ErrorResponse errorResponse = errorHandler.handelErrorResponse(ex);
return errorResponse.toString();
}
}
控制器有错误处理机制,在错误处理选项中,它总是返回期望失败状态代码417.但是我需要设置动态错误Http状态代码,如500,403等,具体取决于错误类型。我该怎么做?
The controller has an error handling mechanism, in the error handling option it always return expectation fail status code 417. But I need to set a dynamic error Http status code like 500, 403 etc depending on type of error. How do I do this?
推荐答案
您需要更改输出值 ResponseEntity 的类型。在这里回答:
如何在Spring MVC中响应HTTP 400错误@ResponseBody方法返回String?
You need to change the type of the output value ResponseEntity. Answer here: How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
这篇关于使用spring 3 restful以编程方式更改http响应状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文