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

标签 spring spring-boot

新手问题...我正在构建我的第一个 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/

相关文章:

java - Spring Boot Security - 默认映射的匿名用户访问/

java - 带有 spring 数据的六边形架构

java - 使用 Spring Framework 3 将文件上传到服务器并将文件存储到磁盘

java - 在运行时覆盖 `spring.cloud.config.profile`

java - 错误: org. springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException

java - 在 Spring 中使用 RestTemplate 的 MultipartFile + String 作为请求参数

java - 如何使用Spring在Java中将一个属性使用到另一个属性中?

java - Spring中实例DAI与Singleton Bean的交互

spring - 无法自动接线。有多个 'DataSource' 类型的 bean

java - 在 Spring Boot 的单元测试期间将配置对象注入(inject)到服务 bean