在Spring Boot中返回状态码202的HTTP响应

时间:2018-12-30 10:44:56

标签: java spring-boot http-status-codes

如何返回状态码202的HTTP响应?

我在Spring Boot文档中找不到任何参考。

3 个答案:

答案 0 :(得分:4)

返回HTTPStatus ACCEPTED,例如:

 return new ResponseEntity<>(HttpStatus.ACCEPTED);
  

202已接受。

答案 1 :(得分:1)

您可以这样做(我正在使用kotlin):

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

或简单的方法是:

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

ResponseEntity.accepted返回202 ResponseEntity.created返回201 等等

答案 2 :(得分:-1)

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并在原因字段中提供一些文本。然后从另一个类或在需要时调用此方法。 (不需要扩展部分)