I am using Spring Boot for a simple REST API and would like to return a correct HTTP statuscode if something fails.
我使用Spring Boot作为一个简单的REST API,并希望在出现故障时返回正确的HTTP状态代码。
@RequestMapping(value="/rawdata/", method = RequestMethod.PUT)
@ResponseBody
@ResponseStatus( HttpStatus.OK )
public RestModel create(@RequestBody String data) {
// code ommitted..
// how do i return a correct status code if something fails?
}
Being new to Spring and Spring Boot, the basic question is how do i return different status codes when something is ok or fails?
作为Spring和Spring Boot的新手,基本的问题是,当某些东西正常或失败时,我如何返回不同的状态代码?
3 个解决方案
#1
74
There are several options you can use. Quite good way is to use exceptions and class for handling called @ControllerAdvice
:
您可以使用多种选项。相当不错的方法是使用异常和类来处理名为@ControllerAdvice的处理:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ResponseStatus(HttpStatus.CONFLICT) // 409
@ExceptionHandler(DataIntegrityViolationException.class)
public void handleConflict() {
// Nothing to do
}
}
Also you can pass HttpServletResponse
to controller method and just set response code:
您也可以将HttpServletResponse传递给控制器方法,只需设置响应代码:
public RestModel create(@RequestBody String data, HttpServletResponse response) {
// code ommitted..
response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
Please refer to great blog post for details: http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
有关详细信息,请参阅精彩博文:http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
note: In Spring Boot using @ResponseBody
annotation is redundant - it's included in @RestController
注意:在Spring Boot中使用@ResponseBody注释是多余的 - 它包含在@RestController中
#2
9
One of the way to do this is you can use ResponseEntity as a return object.
其中一种方法是使用ResponseEntity作为返回对象。
@RequestMapping(value="/rawdata/", method = RequestMethod.PUT)
public ResponseEntity<?> create(@RequestBody String data) {
if(everything_fine)
return new ResponseEntity<>(RestModel, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
#3
0
Try this code:
试试这段代码:
@RequestMapping(value = "/validate", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<ErrorBean> validateUser(@QueryParam("jsonInput") final String jsonInput) {
int numberHTTPDesired = 400;
ErrorBean responseBean = new ErrorBean();
responseBean.setError("ERROR");
responseBean.setMensaje("Error in validation!");
return new ResponseEntity<ErrorBean>(responseBean, HttpStatus.valueOf(numberHTTPDesired));
}
#1
74
There are several options you can use. Quite good way is to use exceptions and class for handling called @ControllerAdvice
:
您可以使用多种选项。相当不错的方法是使用异常和类来处理名为@ControllerAdvice的处理:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ResponseStatus(HttpStatus.CONFLICT) // 409
@ExceptionHandler(DataIntegrityViolationException.class)
public void handleConflict() {
// Nothing to do
}
}
Also you can pass HttpServletResponse
to controller method and just set response code:
您也可以将HttpServletResponse传递给控制器方法,只需设置响应代码:
public RestModel create(@RequestBody String data, HttpServletResponse response) {
// code ommitted..
response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
Please refer to great blog post for details: http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
有关详细信息,请参阅精彩博文:http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
note: In Spring Boot using @ResponseBody
annotation is redundant - it's included in @RestController
注意:在Spring Boot中使用@ResponseBody注释是多余的 - 它包含在@RestController中
#2
9
One of the way to do this is you can use ResponseEntity as a return object.
其中一种方法是使用ResponseEntity作为返回对象。
@RequestMapping(value="/rawdata/", method = RequestMethod.PUT)
public ResponseEntity<?> create(@RequestBody String data) {
if(everything_fine)
return new ResponseEntity<>(RestModel, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
#3
0
Try this code:
试试这段代码:
@RequestMapping(value = "/validate", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<ErrorBean> validateUser(@QueryParam("jsonInput") final String jsonInput) {
int numberHTTPDesired = 400;
ErrorBean responseBean = new ErrorBean();
responseBean.setError("ERROR");
responseBean.setMensaje("Error in validation!");
return new ResponseEntity<ErrorBean>(responseBean, HttpStatus.valueOf(numberHTTPDesired));
}