简体   繁体   English

使用Google Cloud Endpoints发送HTTP 201响应

[英]Send HTTP 201 response with Google Cloud Endpoints

I would like to send a 201 HTTP response after a resource is created along with its location. 创建资源及其位置后,我想发送201 HTTP响应。

HTTP/1.1 201 Created
Location: http://www.example.org/myresource/12546

How do you define your @ApiMethod ? 您如何定义您的@ApiMethod

As stated in the Documentation : 文档所述

HTTP 200 is typically assumed by Endpoints if the API method returns successfully. 如果API方法成功返回,则Endpoints通常假定使用HTTP 200。 If the API method response type is void or the return value of the API method is null , HTTP 204 will be set instead. 如果API方法的响应类型为void或API方法的返回值为null,则将改为设置HTTP 204。 HTTP 2xx codes should not be used in custom exception classes. HTTP 2xx代码不应在自定义异常类中使用。

having said that if you still need to return a 201 code, you can hack a service exception to provide that. 话虽如此,如果您仍然需要返回201代码,则可以破解服务异常以提供该代码。

public class EverythingOKException extends ServiceException {
    public EverythingOKException(String message) {
        super(201, message);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM