Return Spring 引导中状态代码为 202 的 HTTP 响应

Return HTTP response with status code 202 in Spring Boot

如何 return 状态代码为 202 的 HTTP 响应?

我在 Spring 引导文档中找不到它的任何参考。

Return HTTPStatus ACCEPTED,例如:

 return new ResponseEntity<>(HttpStatus.ACCEPTED);

202 Accepted.

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;
}

除了写 NOT_FOUND ,您可以写 ACCEPTED 并在原因字段中提供一些文本。然后从另一个 class 或需要时调用此方法。 (不需要扩展部分)

你可以这样做(我使用的是 kotlin):

return ResponseEntity.accepted().body(OrderResponse().order(order))

或者简单的方法是:

    return ResponseEntity.status(HttpStatus.CREATED).body(OrderResponse().order(order))

ResponseEntity.accepted returns 202 ResponseEntity.created returns 201 等等