Jun 01, 2021 Article blog
There are several types of return results after calling the back-end service, such as
String
Integer
Boolean
List
Map
etc., and in one project, in order to maintain consistency, our method return results can all use
JSON
data format, as follows:
{
"code":200,
"msg":"成功",
"data":"JSON数据"
}
code
is the status code corresponding to the results of this request processing,
msg
is the interpretation information corresponding to the status code,
data
is the content of the data to be returned, can be any one of the objects.
Encapsulates the response information object
public class ResponseEntity<T> implements Serializable {
private static final long serialVersionUID = 3595741978061989861L;
private Integer code;//状态码
private String msg;//状态码对应信息
private T data;//要返回的数据
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 several states,
Http
request status codes, and are enumerated, as in the following example:
public enum ResponseEnum {
SUCCESS(200, "成功"),
FAIL(-1, "失败"),
ERROR_400(400, "错误的请求"),
ERROR_404(404, "访问资源不存在"),
ERROR_500(500, "服务器异常");
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 {
/**
* 成功返回
* @param object 返回数据
* @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;
}
/**
* 成功返回 无数据
* @return
*/
public static ResponseEntity success(){
return success(null);
}
/**
* 失败返回
* @param responseEnum 响应标识
* @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
SpringMvc
controller and needs to be used in conjunction with
@ResponseBody
annotations to return
JSON
object data;
@RestController
is primarily used to build
Restful
interface that returns request data from the client, equivalent to using
@Controller
and
@ResponseBody
annotations.
(Recommended course: Spring tutorial)
Create a
Pojo
package and the corresponding entity class
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
@Controller
public class DemoController {
@RequestMapping(value = "/users", method= RequestMethod.GET)
@ResponseBody
public ResponseEntity users(){
List<DemoEntity> list = new ArrayList<DemoEntity>();
DemoEntity demo = new DemoEntity();
demo.setId(1);
demo.setName("蜗牛");
list.add(demo);
DemoEntity demo1 = new DemoEntity();
demo1.setId(2);
demo1.setName("葡萄");
list.add(demo1);
return ResponseUtil.success(list);
}
}
Test: After you start the service, enter
http://localhost:8080/users
in the browser address bar and you can see the output on the page
{"code":200,"msg":"成功","data":[{"id":1,"name":"蜗牛"},{"id":2,"name":"葡萄"}]}
@RestController
@RestController
public class DemoRestController {
@RequestMapping(value = "/users1", method= RequestMethod.GET)
public ResponseEntity users(){
List<DemoEntity> list = new ArrayList<DemoEntity>();
DemoEntity demo = new DemoEntity();
demo.setId(1);
demo.setName("蜗牛");
list.add(demo);
DemoEntity demo1 = new DemoEntity();
demo1.setId(2);
demo1.setName("葡萄");
list.add(demo1);
return ResponseUtil.success(list);
}
}
You can also see the results above after the request.
(Recommended tutorial: Spring Boot those things.) )
The above is about SpringBoot: Unified JSON information return related to the introduction, I hope to help you.