简体   繁体   English

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

[英]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 文档中找不到任何参考。

Return HTTPStatus ACCEPTED , eg:返回 HTTPStatus ACCEPTED ,例如:

 return new ResponseEntity<>(HttpStatus.ACCEPTED);

202 Accepted. 202 接受。

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.

相关问题 从 spring 引导自定义验证器返回不同的 HTTP 状态代码 - Return different HTTP status code from spring boot custom validators 在 Java Spring Boot 中创建并返回自定义 Http 响应代码 - Create and Return a custom Http response code in Java Spring Boot 预检响应在Angle 4和Spring Boot(RestAPI)应用程序中具有无效的HTTP状态代码403 - Response for preflight has invalid HTTP status code 403 in angular 4 & spring boot (RestAPI) application Spring Boot 自定义 ErrorAttributes http 状态未设置为响应 - Spring Boot custom ErrorAttributes http status not set to response Spring 启动 API 返回 HTTP 302 状态码 - Spring Boot API returning HTTP 302 status code Spring Boot和安全性:如何扩展302状态代码的响应? - Spring Boot and security: How to extend response for 302 status code? 如何更改 Spring Boot 错误响应中的状态代码? - How to change Status code in spring boot error response? 如何在 Spring Boot @ResponseBody 中返回 404 响应状态 - 方法返回类型是 Response? - How to return 404 response status in Spring Boot @ResponseBody - method return type is Response? 返回1xx状态代码时Spring Boot请求挂在那里 - Spring boot request hang there when return 1xx status code Spring 启动:RestTemplate POST 调用不返回创建状态码 - Spring boot : RestTemplate POST call does not return created status code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM