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

What are the encapsulation methods for the results returned by SpringBoot API

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what are the encapsulation methods of the results returned by the SpringBoot interface. I hope you will get something after reading this article. Let's discuss it together.

The rest interface returns all kinds of data, and it is easy to cause confusion if there are no restrictions on the format of the interface.

In a real project, you usually put the results in a wrapper class that contains http status values, status messages, and actual data. Here are two main ways to record: (the effect is as follows)

1. Use the Map object as the return object. / * Http request API result encapsulation method * * @ param object data object * @ param msgSuccess prompt message (request successful) * @ param msgFailed prompt message (request failed) * @ param isOperate whether the operation type (add, delete, modify) * @ return * / public static Map getResponse (Object object, String msgSuccess, String msgFailed, boolean isOperate) {Map data = new HashMap () If (object! = null) {data.put ("success", true); data.put ("msg", msgSuccess); data.put ("result", 1);} else {data.put ("success", false); data.put ("msg", msgFailed); data.put ("result", 0) } if (! isOperate) {data.put ("data", object);} else {data.put ("data", null);} return data;} 2. Call different wrapper methods according to different types of results. / * * format request result (List type) * * @ param * @ param objectList object list * @ param msgSuccess Information prompt (request successful) * @ param msgFailed Information prompt (request failed) * @ return * / @ SuppressWarnings ("unchecked") public static Tb_Response getResponse (List objectList, String msgSuccess, String msgFailed) {Tb_Response tb_Response = new Tb_Response () If (objectList.size () > 0) {tb_Response.setSuccess (true); tb_Response.setMsg (msgSuccess); tb_Response.setResult (1); tb_Response.setData ((List) objectList);} else {tb_Response.setSuccess (false); tb_Response.setMsg (msgFailed); tb_Response.setResult (0);} return (Tb_Response) tb_Response } / * format request result (Object type) * * @ param * @ param object object information * @ param msgSuccess message prompt (request successful) * @ param msgFailed message prompt (request failed) * @ return * / @ SuppressWarnings ("unchecked") public static Tb_ResponseData getResponseData (T object, String msgSuccess, String msgFailed) {Tb_ResponseData tb_ResponseData = new Tb_ResponseData () If (object! = null) {tb_ResponseData.setSuccess (true); tb_ResponseData.setMsg (msgSuccess); tb_ResponseData.setResult (1); tb_ResponseData.setData ((T) object);} else {tb_ResponseData.setSuccess (false); tb_ResponseData.setMsg (msgFailed); tb_ResponseData.setResult (0); tb_ResponseData.setData (null) } return (Tb_ResponseData) tb_ResponseData;} / * format request result (Boolean type) * * @ param result request result * @ param msgSuccess information prompt (request successful) * @ param msgFailed information prompt (request failed) * @ return * / public static Tb_ResponseData getResponseData (boolean result, String msgSuccess, String msgFailed) {Tb_ResponseData tb_ResponseData = new Tb_ResponseData () If (result) {tb_ResponseData.setSuccess (true); tb_ResponseData.setMsg (msgSuccess); tb_ResponseData.setResult (1);} else {tb_ResponseData.setSuccess (false); tb_ResponseData.setMsg (msgFailed); tb_ResponseData.setResult (0);} tb_ResponseData.setData (null); return tb_ResponseData } after reading this article, I believe you have a certain understanding of "what are the encapsulation methods of SpringBoot interface return results". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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