首页 > 其他分享 >如何在Spring Boot Rest服务方法中设置响应头值

如何在Spring Boot Rest服务方法中设置响应头值

时间:2022-08-20 13:25:01浏览次数:71  
标签:头值 Spring Boot Rest ResponseEntity responseHeaders

如何在Spring Boot Rest服务方法中设置响应头值

问题描述

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

推荐答案

来自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);
  // 或者
  return ResponseEntity.status(HttpStatus.CREATED).headers(responseHeaders)
            .body("Hello World");

}

 

标签:头值,Spring,Boot,Rest,ResponseEntity,responseHeaders
From: https://www.cnblogs.com/lizm166/p/16607566.html

相关文章

  • 通过Spring官网创建Spring Boot项目
    spring官网:https://start.spring.io/1、首先,在官网创建springboot项目:2、创建完毕后单击“GENERATE”网页会自动下载项目压缩包:3、解压demo.zip文件夹:4、打开IDEA......
  • SpringBoot整合Redis实现常用功能
    SpringBoot整合Redis实现常用功能建议大小伙们,在写业务的时候,提前画好流程图,思路会清晰很多。文末有解决缓存穿透和击穿的通用工具类。1登陆功能我想,登陆功能......
  • IDEA初始化创建SpringBoot项目
    创建SpringBoot打开新建项目配置项目基本需求其中URL改成:start.springboot.io创建完成后等待依赖包下载下载完成之后点击右侧Maven中的Lifecycle-install进行更新......
  • Spring 02: Spring接管下的三层项目架构
    业务背景需求:使用三层架构开发,将用户信息导入到数据库中目标:初步熟悉三层架构开发核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别注意......
  • SpringBoot中调用Kafka
    Kafka实战——在SpringBoot中的应用官网文档链接1.pom引用 <dependency><groupId>org.springframework.kafka</groupId><artifactId>spri......
  • Spring Security登录的流程
    SpringSecurity登录的流程1、UsernamePasswordAuthenticationFilter这过滤器开始attemptAuthentication方法请求的request中的参数setDetails(request,authReque......
  • spring boot thymeleaf 不能访问templates目录下的页面问题
    springboot默认情况下可以直接访问四个目录下的静态文件(https://www.cnblogs.com/realzhaijiayu/p/16566667.html)publicstaticresourcesMETA-INF/resources引入thym......
  • SpringWebflux框架里导出excel文档
    本demo里使用springboot2.7.0版本。@GetMapping("/download/excel/file")publicMono<Void>downloadExcelFile(ServerHttpResponseresponse,WebSessionwebSessi......
  • spring5 ioc 管理bean 注解
    1.注解种类@Component(value="student")@Service@Repository@Controller 2.使用注解扫描包<context:component-scanbase-package="com.cj"></context:componen......
  • 【文件上传】Spring MVC 文件上传
    (1)文件上传三要素:1、表单的提交方式method="POST"2、表单的enctype属性是多部分表单形式enctype=“multipart/form-data"3、表单项(元素)type="file"<formaction=""m......