如何在 Spring Boot Rest api 响应的 ResponseEntity 中添加自定义属性

     2023-02-26     248

关键词:

【中文标题】如何在 Spring Boot Rest api 响应的 ResponseEntity 中添加自定义属性【英文标题】:How to add custom attributes in ResponseEntity of Spring Boot Rest api response 【发布时间】:2020-05-09 16:57:53 【问题描述】:

由于 spring boot 提供了 ResponseEntity 来表示对 rest api 的 HTTP 响应,包括 headers、body 和 status。

我的 RestController 包含 getTodoById 方法,如下所示-

@GetMapping("/todo/id")
 public Todo getTodoById(@PathVariable String id) 
        int todoId = Integer.parseInt(id);
        Todo todoItem = todoRepository.findById(todoId);
         ResponseEntity.ok(todoItem);
    

它在 api hit(api/v1/todo/13) 上给出以下 api 响应。


    "id": 13,
    "title": "title13",
    "status": "not started"

应用程序中的所有 api 都需要有一个通用的自定义响应结构,如下所示-


  "status": "success",
  "data": 
    "id": 13,
    "title": "title13",
    "status": "not started"
  ,
  "error": null,
  "statusCode": 200



  "status": "failure",
  "data": ,
  "error": "bad request",
  "statusCode": 400

如何使用 ResponseEntity 获得所需的 JSON 响应结构?

我探索了它,但找不到解决上述问题的解决方案。

任何帮助将不胜感激。 谢谢

【问题讨论】:

也许你可以从Spring Boot's tracing feature那里得到一些灵感? 【参考方案1】:

好吧,而不是返回

ResponseEntity.ok(todoItem);

你显然需要返回类似的东西

ResponseEntity.ok(new Response(todoItem));

public class Response 

    private String status = "success";

    private Object data;

    private String error;

    private int statusCode = 200;

    // Constructor, getters and setters omitted

【讨论】:

如何在 Spring Boot REST API 中格式化返回的 json?

】如何在SpringBootRESTAPI中格式化返回的json?【英文标题】:HowtoformatreturnedjsoninSpringBootRESTAPI?【发布时间】:2021-05-1611:31:25【问题描述】:我使用SpringBoot创建了以下RESTAPI。@RestControllerpublicclassPersonController@AutowiredprivatePersonRepository... 查看详情

如何在 Spring Boot Rest API 中以 XML 形式返回对象列表

】如何在SpringBootRestAPI中以XML形式返回对象列表【英文标题】:HowtoreturnalistofobjectsasXMLinSpringbootrestAPI【发布时间】:2021-09-2305:57:43【问题描述】:我正在编写一个Java网络服务,它将以以下XML格式返回产品列表。<?xmlversion="1.0"?&... 查看详情

如何在 Spring Boot Rest api 响应的 ResponseEntity 中添加自定义属性

】如何在SpringBootRestapi响应的ResponseEntity中添加自定义属性【英文标题】:HowtoaddcustomattributesinResponseEntityofSpringBootRestapiresponse【发布时间】:2020-05-0916:57:53【问题描述】:由于springboot提供了ResponseEntity来表示对restapi的HTTP响应,... 查看详情

如何在 Spring Boot Rest api 中创建安全登录控制器

】如何在SpringBootRestapi中创建安全登录控制器【英文标题】:Howtocreatesecurelogincontrollerinspringbootrestapi【发布时间】:2016-06-0323:56:27【问题描述】:我想使用SpringBootRESTAPI创建用于登录的控制器。但我不知道该怎么做。我想为此使... 查看详情

在 Spring-boot 中成功登录后如何限制 POST Rest Api 以供公共访问

】在Spring-boot中成功登录后如何限制POSTRestApi以供公共访问【英文标题】:HowtorestrictPOSTRestApiforpublicaccessaftersuccessfullogininSpring-boot【发布时间】:2020-07-1304:31:17【问题描述】:我是JAVA/Springboot的新手,我想限制POSTrestapi公开访问。... 查看详情

如何在 Spring Boot REST API 中为 db insert 方法编写模拟单元测试?

】如何在SpringBootRESTAPI中为dbinsert方法编写模拟单元测试?【英文标题】:HowtowriteamockedunittestforadbinsertmethodinSpringbootRESTAPI?【发布时间】:2021-05-1818:51:06【问题描述】:我有一个用SpringBoot编写的RESTAPI。现在我想用JUnit和Mockito创建... 查看详情

如何在 Spring Boot 中制作可以重定向到 url 的 REST api

】如何在SpringBoot中制作可以重定向到url的RESTapi【英文标题】:HowtomakeRESTapithatcanredirecttourlinSpringBoot【发布时间】:2021-08-0819:51:45【问题描述】:我创建了一个RESTapi,它可以用来保存不同的url,这些url具有自动递增功能,可以为... 查看详情

Spring Boot JWT - 如何实现刷新令牌和注销 REST-API

】SpringBootJWT-如何实现刷新令牌和注销REST-API【英文标题】:SpringBootJWT-HowtoimplementRefreshTokenandLogoutREST-API【发布时间】:2021-03-1113:44:10【问题描述】:我们决定从Basic-Auth切换到JWT,因为session-id存储在内存中,有时会导致拍摄时内... 查看详情

如何在 Spring Boot REST API 中启用对 JSON / Jackson @RequestBody 的严格验证?

】如何在SpringBootRESTAPI中启用对JSON/Jackson@RequestBody的严格验证?【英文标题】:HowdoIenablestrictvalidationofJSON/Jackson@RequestBodyinSpringBootRESTAPI?【发布时间】:2019-09-2620:49:04【问题描述】:如果在JSON请求中指定了额外的参数,我该如何... 查看详情

如何使用 Spring Boot 应用程序从 Rest API 返回 html

】如何使用SpringBoot应用程序从RestAPI返回html【英文标题】:HowtoreturnhtmlfromRestAPIwithspringbootapplication【发布时间】:2018-03-0114:33:39【问题描述】:我需要在SpringBootApplication中从RestAPI返回一个html页面。html"test.html"位于src/main/... 查看详情

如何在 Spring Boot 中使用带有 Bearer Token 和 form-data 的 Rest Template 调用 REST Api

】如何在SpringBoot中使用带有BearerToken和form-data的RestTemplate调用RESTApi【英文标题】:HowtocallaRESTApiusingRestTemplatewithBearerTokenandform-datainSpringboot【发布时间】:2020-06-2223:18:52【问题描述】:我正在使用Postman调用restapi,它使用以下请... 查看详情

如何在外部 tomcat 中部署 Spring-boot REST API

】如何在外部tomcat中部署Spring-bootRESTAPI【英文标题】:HowtodeploySpring-bootRESTAPIinexternaltomcat【发布时间】:2017-11-2301:31:51【问题描述】:我有一个没有任何网页的Spring-bootgradle应用程序。这个应用程序有RESTapi,当我将它作为Springboo... 查看详情

如何在使用 OAuth2 社交登录保护 REST API 的同时配置 Spring Boot 登录页面

】如何在使用OAuth2社交登录保护RESTAPI的同时配置SpringBoot登录页面【英文标题】:HowtoconfigureSpringbootloginpagewhilesecuringRESTAPIusingOAuth2sociallogin【发布时间】:2021-11-0523:04:13【问题描述】:我有一个使用Google的SpringOAuth2身份验证的示... 查看详情

如何使用 MySQL 在 Spring Boot REST API 中放置和获取任何格式的 JSON 对象?

】如何使用MySQL在SpringBootRESTAPI中放置和获取任何格式的JSON对象?【英文标题】:HowdoIPUTandGETJSONobjectofANYformatinaSpringBootRESTAPIusingMySQL?【发布时间】:2021-11-1610:32:54【问题描述】:我需要能够将来自POSTMAN的JSON数据(没有固定格式... 查看详情

如何从 JSON 数组在 DB 中创建表以在 Spring Boot 中创建 REST API

】如何从JSON数组在DB中创建表以在SpringBoot中创建RESTAPI【英文标题】:HowtocreateatableinDBfromaJSONarraytomakeaRESTAPIinSpringBoot【发布时间】:2021-05-1704:17:14【问题描述】:大家下午好。我从SpringJSON和APIRest开始,我正在做我的第一次练习... 查看详情

ControllerAdvice 的异常处理程序在使用 Spring Boot 的 Rest API 获取请求中不起作用。如何解决?

...常处理程序在使用SpringBoot的RestAPI获取请求中不起作用。如何解决?【英文标题】:ExceptionHandlerofControllerAdviceNotworkinginRestAPIGetRequestusingSpringBoot.Howtoresolve?【发布时间】:2021-09-2012:20:11【问题描述】:我有一个使用SpringBoot的RestCon... 查看详情

如何在客户端使用 React 的 BrowserRouter 并在服务器上使用 Java REST API (Spring Boot)?

】如何在客户端使用React的BrowserRouter并在服务器上使用JavaRESTAPI(SpringBoot)?【英文标题】:HowtouseReact\'sBrowserRouteronclientandJavaRESTAPI(SpringBoot)ontheserver?【发布时间】:2017-09-3018:23:44【问题描述】:我想在客户端使用React并在服务器... 查看详情

在 Spring Boot REST API 中上传文件

】在SpringBootRESTAPI中上传文件【英文标题】:UploadfileinSpringBootRESTAPI【发布时间】:2018-10-0823:54:54【问题描述】:我正在尝试使用SpringBoot@RestController上传文件:@RequestMapping(value="/register",method=RequestMethod.POST)publicAppResponseregisterUserFr 查看详情