Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to unify the return of JSON information by SpringBoot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how SpringBoot unifies JSON information return". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "SpringBoot how to unify JSON information return" can help you solve the problem.

There are many types of results returned after calling the backend service, such as String,Integer,Boolean,List,Map, etc. In a project, in order to maintain unity, the returned results of our method can all use JSON data format, as follows:

{"code": 200, "msg": "success", "data": "JSON data"}

Where code is the status code corresponding to the processing result of this request, msg is the interpretation information corresponding to the status code, and data is the data content to be returned, which can be any object.

Encapsulate response information object

Public class ResponseEntity implements Serializable {private static final long serialVersionUID = 3595741978061989861L; private Integer code;// status code private String msg;// status code correspondence information private T data;// data to be returned public Integer getCode () {return code;} public void setCode (Integer code) {this.code = code;} public String getMsg () {return msg } public void setMsg (String msg) {this.msg = msg;} public T getData () {return data;} public void setData (T data) {this.data = data;}}

There are many status codes for Http requests. Enumeration is used as an example:

Public enum ResponseEnum {SUCCESS (200, "success"), FAIL (- 1, "failure"), ERROR_400 (400, "incorrect request"), ERROR_404 (404, "access resource does not exist"), ERROR_500 (500, "server exception"); private Integer code; private String msg; ResponseEnum (Integer code, String msg) {this.code = code; this.msg = msg } public Integer getCode () {return code;} public void setCode (Integer code) {this.code = code;} public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg;}}

Create a public class to generate a response object

Public class ResponseUtil {/ * successfully returned * @ param object returned data * @ return * / public static ResponseEntity success (Object object) {ResponseEntity resp = new ResponseEntity (); resp.setCode (ResponseEnum.SUCCESS.getCode ()); resp.setMsg (ResponseEnum.SUCCESS.getMsg ()); resp.setData (object); return resp } / * numerous data * @ return * / public static ResponseEntity success () {return success (null);} / * failed return * @ param responseEnum response ID * @ return * / public static ResponseEntity error (ResponseEnum responseEnum) {ResponseEntity resp = new ResponseEntity (); resp.setCode (responseEnum.getCode ()) Resp.setMsg (responseEnum.getMsg ()); return resp;}

Controllers in Spring can be declared with @ Controller and @ RestController annotations, where @ Controller identifies that the current controller is a SpringMvc controller and needs to be used in conjunction with @ ResponseBody annotation to return JSON object data. @ RestController is mainly used to build Restful-style interfaces and return client request data, which is equivalent to using @ Controller and @ ResponseBody annotations at the same time.

(recommended course: Spring tutorial)

Create Pojo packages and corresponding entity classes

Public class DemoEntity {private Integer id; private String name; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;}}

Create a controller

@ Controller

@ Controllerpublic class DemoController {@ RequestMapping (value = "/ users", method= RequestMethod.GET) @ ResponseBody public ResponseEntity users () {List list = new ArrayList (); DemoEntity demo = new DemoEntity (); demo.setId (1); demo.setName ("snail"); list.add (demo); DemoEntity demo1 = new DemoEntity (); demo1.setId (2); demo1.setName ("grape") List.add (demo1); return ResponseUtil.success (list);}}

Test: after starting the service, type http://localhost:8080/users in the browser address bar to see the output information on the page

{"code": 200,200 "msg": "success", "data": [{"id": 1, "name": "snail"}, {"id": 2, "name": "grape"}]}

@ RestController

@ RestControllerpublic class DemoRestController {@ RequestMapping (value = "/ users1", method= RequestMethod.GET) public ResponseEntity users () {List list = new ArrayList (); DemoEntity demo = new DemoEntity (); demo.setId (1); demo.setName ("snail"); list.add (demo); DemoEntity demo1 = new DemoEntity (); demo1.setId (2); demo1.setName ("grape") List.add (demo1); return ResponseUtil.success (list);}}

The above results can also be seen after the request.

This is the end of the introduction to "how SpringBoot unifies the return of JSON information". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report