[英]Return HTTP response with status code 202 in Spring Boot
How can I return HTTP response with status code 202?如何返回状态码为 202 的 HTTP 响应?
I couldn't find any reference for it in Spring Boot documentation.我在 Spring Boot 文档中找不到任何参考。
You can do like this (I am using kotlin) :你可以这样做(我正在使用 kotlin):
return ResponseEntity.accepted().body(OrderResponse().order(order))
OR Easy way is:或简单的方法是:
return ResponseEntity.status(HttpStatus.CREATED).body(OrderResponse().order(order))
ResponseEntity.accepted returns 202 ResponseEntity.created returns 201 and so on ResponseEntity.accepted 返回 202 ResponseEntity.created 返回 201 依此类推
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No Article found with this Article Number.")
public class NoArticleFoundException extends RuntimeException{
private static final long serialVersionUID =3935230281455340039L;
}
Instead of writing NOT_FOUND , you can write ACCEPTED and give some text in reason field.您可以编写 ACCEPTED 并在原因字段中提供一些文本,而不是编写 NOT_FOUND 。 And then call this method from another class or when ever required.然后从另一个类或在需要时调用此方法。 (extends part is not required) (不需要扩展部分)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.