gpt4 book ai didi

spring - 如何在 Spring Boot 休息服务方法中设置响应 header value ?

转载 作者:IT老高 更新时间:2023-10-28 13:55:59 27 4
gpt4 key购买 nike

新手问题...我正在构建我的第一个 Spring Boot Restful 服务。我的 Restful 服务设计需要在响应 header 中返回一些数据。

如何在我的 Controller 类方法中设置响应 header 值?

最佳答案

来自 Spring 文档:

@RequestMapping("/handle")
public ResponseEntity<String> handle() {
URI location = ...;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(location);
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}

来源:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

关于spring - 如何在 Spring Boot 休息服务方法中设置响应 header value ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com