Spring MVC返回前端消息体ReturnMsg

Spring MVC 返回前端代码格式有多中,项目开发人员多,这里提供一个统一的返回实体格式,@RestController会自动转为json格式的数据

1.Controller实例代码

package com.qcsoft.qcofflinereport.controller;

import com.qcsoft.qcofflinereport.message.ReturnMsg;
import com.qcsoft.qcofflinereport.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: fangzh
 * @CreateDate: 2020-03-18 16:02
 */
@RestController
@RequestMapping("test")
public class TestController {
     
    
    @Autowired
    private ReportService reportService;
    /**
     * 查询用户信息
     * @return
     */
    @RequestMapping(value = "selectinfo",method = RequestMethod.GET)
    public ReturnMsg selectInfo(){
     
        ReturnMsg returnMsg=new ReturnMsg();
        List<String> stringList=reportService.getTodayReportInfo();
        returnMsg.setData(stringList);
        return returnMsg;
    }
}

2.Spring MVC返回前端消息体ReturnMsg

// An highlighted block



import java.io.Serializable;

/**
 * controller等接收ajax调用,返回消息体
 * @author 
 *
 */
public class ReturnMsg implements Serializable {
     
	private static final long serialVersionUID = 2017120301;
	private Integer flag;
	private String message;
	private Object data;	//数据,可以是PageBean
	public final static int OK=1;
	public final static int FAIL = 0;
	public final static int CODE = 2;
	public final static int NOT_LOGIN=-1;
	public final static int TOO_OLD=3;
	public final static int NOT_GG=4;//没有关注公众号

	public ReturnMsg() {
     
		this.flag = OK;
		this.message = "OK";
	}
	
	public ReturnMsg(Object data){
     
		this.flag = OK;
		this.message = "OK";
		this.data = data;
	}

	public ReturnMsg(Integer flag, String message, Object data){
     
		this.flag = flag;
		this.message = message;
		this.data = data;
	}
	
	public boolean isSuccess(){
     
		return this.flag == OK;
	}
	
	
	/**
	 * 构造个操作失败的返回接口
	 * @param message
	 */
	public ReturnMsg(String message){
     
		fail(message);
	}
	
	/**
	 * @param message
	 */
	public ReturnMsg(int flag, String message){
     
		this.flag = flag;
		this.message = message;
	}
	
	
	public ReturnMsg notLogin(){
     
		this.flag = NOT_LOGIN;
		this.message = "请先登录";
		this.data = null;
		return this;
	}
	
	public ReturnMsg fail(String message){
     
		this.flag = FAIL;
		this.message = message;
		return this;
	}

	public ReturnMsg success(String message){
     
		this.flag = OK;
		this.message = message;
		return this;
	}

	

	public Integer getFlag() {
     
		return flag;
	}

	
	public void setFlag(Integer flag) {
     
		this.flag = flag;
	}

	public String getMessage() {
     
		return message;
	}

	public void setMessage(String message) {
     
		this.message = message;
	}

	/**
	 * 强制类型转换
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getData() {
     
		if (data == null) {
     
			return null;
		}
		return (T)data;
	}
	

	public ReturnMsg setData(Object data) {
     
		 this.data = data;
		 return this;
	}
	
}

3.前端展示看到的json:

格式如下
{
     
    "flag": 1,
    "message": "OK",
    "data": {
     
        "listGood": [
            {
     
                "商品名称": "天龙八部",
                "定价": 12.5400,
                "商品编码": "9987000000009",
                "销售金额": 1202529.9200,
                "销售数量": 100135
            }
        ]
    },
    "success": true
}

你可能感兴趣的:(项目功能,java,spring)