Java 从 Spring REST API 返回 HTTP 代码 200
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51735084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return HTTP code 200 from Spring REST API
提问by Peter Penzov
I want to use this code to receive http link with values:
我想使用此代码接收带有值的 http 链接:
@PostMapping(value = "/v1/notification")
public String handleNotifications(@RequestParam("notification") String itemid) {
// parse here the values
return "result successful result";
}
How I can return http code 200
- successful response?
如何返回 http 代码200
- 成功响应?
And also for example if there is a code exception into code processing how can I return error 404
?
并且例如,如果代码处理中存在代码异常,我该如何返回错误404
?
采纳答案by lealceldeiro
You can do it by annotating your method with @ResponseStatus
using HttpStatus.OK
(However it should be 200
by default), like this:
您可以通过@ResponseStatus
使用HttpStatus.OK
(但是200
默认情况下应该是)注释您的方法来做到这一点,如下所示:
Some controller
一些控制器
@PostMapping(value = "/v1/notification")
@ResponseStatus(HttpStatus.OK)
public String handleNotifications(@RequestParam("notification") String itemid) throws MyException {
if(someCondition) {
throw new MyException("some message");
}
// parse here the values
return "result successful result";
}
Now, in order to return a custom code when handling a specific exception you can create a whole separate controller for doing this (you can do it in the same controller, though) which extends from ResponseEntityExceptionHandler
and is annotated with @RestControllerAdvice
and it must have a method for handling that specific exception as shown below:
现在,为了在处理特定异常时返回自定义代码,您可以创建一个完整的单独控制器来执行此操作(尽管您可以在同一个控制器中执行此操作),该控制器扩展自ResponseEntityExceptionHandler
并带有注释,@RestControllerAdvice
并且它必须有一个用于执行此操作的方法处理该特定异常,如下所示:
Exception handling controller
异常处理控制器
@RestControllerAdvice
public class ExceptionHandlerController extends ResponseEntityExceptionHandler {
@ExceptionHandler(MyException.class)
protected ResponseEntity<Object> handleMyException(MyException ex, WebRequest req) {
Object resBody = "some message";
return handleExceptionInternal(ex, resBody, new HttpHeaders(), HttpStatus.NOT_FOUND, req);
}
}
回答by Cristiano Bombazar
If you are using spring:
如果您使用的是弹簧:
@PostMapping(value = "/v1/notification")
public ResponseEntity handleNotifications(@RequestParam("notification") String itemid) {
// parse here the values
return ResponseEntity.ok().build(); //OR ResponseEntity.ok("body goes heare");
}
回答by Manish Karki
You can do something like this:
你可以这样做:
@PostMapping(value = "/v1/notification")
public ResponseEntity<String> handleNotifications(
@RequestParam("notification") String itemid) {
// parse here the values
return new ResponseEntity<>("result successful result",
HttpStatus.OK);
}
回答by dehasi
If you use @RestController
it should return 200 by default.
如果您使用@RestController
它,默认情况下应该返回 200。
But anyway, you can set a particular response status by @ResponseStatus
annotation (even if the methods returns void
) or you can return a custom response by ResponseEntity
.
但无论如何,您可以通过@ResponseStatus
注释设置特定的响应状态(即使方法返回void
),也可以通过 返回自定义响应ResponseEntity
。
EDIT: added error handling
编辑:添加错误处理
For error handling, you can return a particular response entity:
对于错误处理,您可以返回特定的响应实体:
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body("some body ");
or you can use @ExceptionHandler
:
或者你可以使用@ExceptionHandler
:
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleError(Exception ex) {
// TODO: log exception
}