目录
基本内容
一种企业级的项目开发规范-项目前后端通信(或者与第三方服务通信)时的一种约定
准备
在api模块》src》main》java》com.cuihua.boot.middleware.api.response下创建一个状态码枚举类StatusCode.和BaseResponse(项目的统一响应模型类)
package com.cuihua.boot.middleware.api.response;
public enum StatusCode {
Success(0,"成功"),
Fail(-1, "失败"),
InvalidParam(201, "非法的参数!")
;
private Integer code;
private String msg;
StatusCode(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;
}
}
package com.cuihua.boot.middleware.api.response;
//为了支持各种各样的数据类型,使用了泛型
public class BaseResponse<T> {
private Integer code;
private String msg;
private T data;
public BaseResponse(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public BaseResponse(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public BaseResponse(StatusCode statusCode) {
//体现封装的思想
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
}
案例一:不返回数据
在server的controller中
package com.cuihua.boot.middleware.server.controller;
import com.cuihua.boot.middleware.api.response.BaseResponse;
import com.cuihua.boot.middleware.api.response.StatusCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("base")
public class BaseController {
@RequestMapping(value="info", method = RequestMethod.GET)
public BaseResponse info(){
BaseResponse response = new BaseResponse(StatusCode.Success);
try{
response.setData("眼睛为他下着雨,心却为他打着伞,这就是爱情~");
}catch (Exception e){
response = new BaseResponse(StatusCode.Fail);
}
return response;
}
}
在postman中可以请求到具体的情况
案例二:返回数据
数据层
package com.cuihua.boot.middleware.server.dto;
import java.io.Serializable;
public class BaseDto implements Serializable {
private Integer id;
private String content;
public BaseDto() {
}
public BaseDto(Integer id, String content) {
this.id = id;
this.content = content;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
service层
package com.cuihua.boot.middleware.server.service;
import com.cuihua.boot.middleware.server.controller.BaseController;
import com.cuihua.boot.middleware.server.dto.BaseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@Service
public class BaseService {
private static final Logger log = LoggerFactory.getLogger(BaseService.class);
public List<BaseDto> getList() throws Exception{
List<BaseDto> dtos = new LinkedList<BaseDto>();
dtos.add(new BaseDto(1, "天空没有翅膀的痕迹,而我已经飞过;思念是翅膀飞过的痕迹。"));
dtos.add(new BaseDto(2, "长日尽处,我站在你的面前,你将看到我的疤痕,知道我曾经受伤,也曾经痊愈。"));
dtos.add(new BaseDto(3, "我的心是旷野的鸟,在你的眼睛里找到了天空。"));
dtos.add(new BaseDto(4, "你的完美,是一种债,我终身偿还,以唯一的爱。"));
dtos.add(new BaseDto(5, "我曾抱有不少幻想,现在我把幻想抛弃。循着虚假的希望的足迹,我踩到了荆棘上,才知道它们不是鲜花,我永不和爱情逢场作戏,我永不戏弄我的心"));
return dtos;
}
}
Controller层
@Autowired
private BaseService baseService;
//列表-请求方法
@RequestMapping(value = "list", method = RequestMethod.GET)
public BaseResponse list() {
BaseResponse response = new BaseResponse(StatusCode.Success);
try{
List<BaseDto> dtoList= baseService.getList();
response.setData(dtoList);
log.info("--列表-请求方法-得到的业务数据:{}, ", dtoList);
}catch (Exception e){
response = new BaseResponse(StatusCode.Fail);
}
return response;
}
结果
给日志抽象,再继承使用
public class AbstractController {
//体现一种封装的思想(日志的统一抽取)
protected Logger log = LoggerFactory.getLogger(getClass());
}
public class BaseController extends AbstractController{