1.响应VO字面量bean拷贝工具
响应VO字面量bean拷贝工具
一、统一响应类和响应枚举
1.响应类
import com.fasterxml.jackson.annotation.JsonInclude;
import com.sangeng.enums.AppHttpCodeEnum;
import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public ResponseResult() {
this.code = AppHttpCodeEnum.SUCCESS.getCode();
this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult() {
ResponseResult result = new ResponseResult();
return result;
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
if(data!=null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums){
return setAppHttpCodeEnum(enums,enums.getMsg());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg){
return setAppHttpCodeEnum(enums,msg);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
return okResult(enums.getCode(),enums.getMsg());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
return okResult(enums.getCode(),msg);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
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;
}
}
2.响应枚举类
public enum AppHttpCodeEnum {
// 成功
SUCCESS(200,"操作成功"),
// 登录
NEED_LOGIN(401,"需要登录后操作"),
NO_OPERATOR_AUTH(403,"无权限操作"),
SYSTEM_ERROR(500,"出现错误"),
USERNAME_EXIST(501,"用户名已存在"),
PHONENUMBER_EXIST(502,"手机号已存在"), EMAIL_EXIST(503, "邮箱已存在"),
REQUIRE_USERNAME(504, "必需填写用户名"),
LOGIN_ERROR(505,"用户名或密码错误");
int code;
String msg;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.msg = errorMessage;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
3.代码实现
使用:
return ResponseResult.okResult(articles);
代码使用流程:
1,public static ResponseResult okResult() {
ResponseResult result = new ResponseResult();
return result;
}
2, public ResponseResult() {
this.code = AppHttpCodeEnum.SUCCESS.getCode();
this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
}
3, public enum AppHttpCodeEnum {
// 成功
SUCCESS(200,"操作成功"),
}
二、跨域问题
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径
registry.addMapping("/**")
// 设置允许跨域请求的域名
.allowedOriginPatterns("*")
// 是否允许cookie
.allowCredentials(true)
// 设置允许的请求方式
.allowedMethods("GET", "POST", "DELETE", "PUT")
// 设置允许的header属性
.allowedHeaders("*")
// 跨域允许时间
.maxAge(3600);
}
}
三、VO优化a返回部分属性复制到aVO中
1.VO类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HotArticleVo {
private Long id;
//标题
private String title;
//访问量
private Long viewCount;
}
2.a类拷贝VO类
List<Article> articles = page.getRecords();//a类
//bean拷贝
List<HotArticleVo> articleVos = new ArrayList<>();//aVO类
for (Article article : articles) {
HotArticleVo vo = new HotArticleVo();
BeanUtils.copyProperties(article,vo);
articleVos.add(vo);
}
return ResponseResult.okResult(articleVos);//返回
四、字面量
实际项目中都不允许直接在代码中使用字面值。都需要定义成常量来使用。这种方式有利于提高代码的可维护性。
public class SystemConstants
{
/**
* 文章是草稿
*/
public static final int ARTICLE_STATUS_DRAFT = 1;
/**
* 文章是正常分布状态
*/
public static final int ARTICLE_STATUS_NORMAL = 0;
}
queryWrapper.eq("Status",SystemConstants.ARTICLE_STATUS_NORMAL);//判断status为ARTICLE_STATUS_DRAFT的字面量为1
五、Bean拷贝工具类封装
1.bean拷贝工具类
public class BeanCopyUtils {
private BeanCopyUtils() {
}
public static <V> V copyBean(Object source,Class<V> clazz) {
//创建目标对象
V result = null;
try {
result = clazz.newInstance();
//实现属性copy
BeanUtils.copyProperties(source, result);
} catch (Exception e) {
e.printStackTrace();
}
//返回结果
return result;
}
public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
return list.stream()
.map(o -> copyBean(o, clazz))//替换流中a对象为avo对象
.collect(Collectors.toList());//搜集成list
}
}
2.代码实现
2.1 对象拷贝成对象
public static void main(String[] args) {
Article article = new Article();
article.setId(122L);
article.setTitle("cs");
HotArticleVo hotArticleVo = copyBean(article, HotArticleVo.class);
System.out.println(hotArticleVo);
}
2.2 集合拷贝成集合
List<Article> articles = page.getRecords();
// List<HotArticleVo> articleVos = new ArrayList<>();
//bean拷贝
// for (Article article : articles) {
// //这里vo如果在for外面new将导致articleVos内数据为同一个vo且为最后一次修改的值
// HotArticleVo vo = new HotArticleVo();
// BeanUtils.copyProperties(article,vo);
// articleVos.add(vo);
// }
//使用bean工具类
List<HotArticleVo> articleVos =BeanCopyUtils.copyBeanList(articles,HotArticleVo.class);
return ResponseResult.okResult(articleVos);
注意点
一、不能在for外部new一个对象后在内部使用
List<HotArticleVo> articleVos = new ArrayList<>();
//bean拷贝
for (Article article : articles) {
//这里vo如果在for外面new将导致articleVos内数据为同一个vo且为最后一次修改的值
HotArticleVo vo = new HotArticleVo();
BeanUtils.copyProperties(article,vo);
articleVos.add(vo);
}

浙公网安备 33010602011771号