springboot----统一接口返回的格式

@Slf4j
public class ResponseFormat {
	
	private ResponseFormat() {}
	
	private static Map messageMap = new HashMap<>();
	
	static {
		messageMap.put("200", "业务成功");
	}
	
	public static JSONObject buildResponseJson(String status, JSONObject responseJson) {
		responseJson.put("code", status);
		responseJson.put("msg", messageMap.get(status));
		log.info("请求返回结果: {}", JSONObject.toJSONString(responseJson));
		return responseJson;
	}
	
	
}

提示:接口返回时直接 return ResponseFormat.buildResponseJson("code值",  "new一个json或者放入需要返回的json")

你可能感兴趣的:(笔记)