更改了响应体包装类,改为ResponseList和ResponseMap两个类包装,添加了BeanUtils和DoNotDeserialize注释

master
ArgonarioD 2022-06-28 22:56:50 +08:00
parent ac10fe63e7
commit d4ef6ea2cf
6 changed files with 248 additions and 18 deletions

View File

@ -0,0 +1,19 @@
package cn.edu.hfut.rmdjzz.projectmanagement.annotation;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.BeanUtils;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* {@link BeanUtils#beanToMap(Object, boolean, boolean)}Map
*
* @author
* @since 2022/6/28 21:42
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface DoNotDeserialize {
}

View File

@ -14,6 +14,6 @@ public class Staff {
private Integer staffId;
private String staffUsername;
private String staffPassword;
private String staffNickname;
private String staffFullname;
private String staffGender;
}

View File

@ -0,0 +1,83 @@
package cn.edu.hfut.rmdjzz.projectmanagement.utils;
import cn.edu.hfut.rmdjzz.projectmanagement.annotation.DoNotDeserialize;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @author
* @since 2022/6/28 21:41
*/
public class BeanUtils {
/**
* beanmap{@link DoNotDeserialize}
*
* @param putNulls nullmap
* @param toUnderline 线
*/
public static Map<String, Object> beanToMap(Object object, boolean toUnderline, boolean putNulls) {
Map<String, Object> map = new HashMap<>();
if (object == null) {
return map;
}
Field[] fields = object.getClass().getDeclaredFields();
Class<?> clazz = object.getClass();
for (Field field : fields) {
String fieldName = field.getName();
if (fieldName.equals("serialVersionUID") || field.getAnnotation(DoNotDeserialize.class) != null) {
continue;
}
String key = toUnderline ? StringUtils.camelToUnderline(fieldName) : fieldName;
try {
PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);
Method method = pd.getReadMethod();
Object value = method.invoke(object);
if (putNulls || value != null) {
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
public static Map<String, Object> beanToMap(Object object, boolean toUnderline) {
return beanToMap(object, toUnderline, true);
}
public static Map<String, Object> beanToMap(Object object) {
return beanToMap(object, true, true);
}
/**
* objectsnull
* @return nulltruefalse
*/
public static boolean checkAllNotNull(Object... objects) {
for (Object object : objects) {
if (object == null) {
return false;
}
}
return true;
}
/**
* objectsnull
* @return truefalse
*/
public static boolean checkAnyNotNull(Object... objects) {
for (Object object : objects) {
if (object != null) {
return true;
}
}
return false;
}
}

View File

@ -1,44 +1,62 @@
package cn.edu.hfut.rmdjzz.projectmanagement.entity;
package cn.edu.hfut.rmdjzz.projectmanagement.utils.response;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.http.HttpStatus;
import java.util.*;
import java.util.stream.Collectors;
/**
* of<br/>
* {@code return ResponseEntity.<User>of(HttpStatus.OK.value(),"查询成功!",result);}
* of<br/>
* {@code return ResponseList.<User>of(HttpStatus.OK.value(),"查询成功!",result);}
*
* @author
* @since 2022/6/28 11:59
*/
@SuppressWarnings({"unchecked", "varargs"})
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ResponseEntity<T> {
public class ResponseList<T> {
private Integer code;
private String msg;
private QueryPage<T> data;
/**
*
* datatotal0recordsdata
*
* @param <T>
* @param code
* @param msg
* @param data
*/
public static <T> ResponseEntity<T> of(Integer code, String msg, T... data) {
return new ResponseEntity<>(code, msg, new QueryPage<>(data));
public static <T> ResponseList<T> of(Integer code, String msg, T... data) {
return new ResponseList<>(code, msg, new QueryPage<>(data));
}
public static <T> ResponseEntity<T> of(Integer code, String msg, Collection<T> data) {
return new ResponseEntity<>(code, msg, new QueryPage<>(data));
public static <T> ResponseList<T> of(Integer code, String msg, Collection<T> data) {
return new ResponseList<>(code, msg, new QueryPage<>(data));
}
public static <T> ResponseEntity<T> of(Integer code, String msg, Page<T> data) {
return new ResponseEntity<>(code, msg, new QueryPage<>(data));
public static <T> ResponseList<T> of(Integer code, String msg, Page<T> data) {
return new ResponseList<>(code, msg, new QueryPage<>(data));
}
/**
*
*/
public static <T> ResponseList<T> ofSuccess(String msg, T... data) {
return of(HttpStatus.OK.value(), msg, data);
}
public static <T> ResponseList<T> ofSuccess(String msg, Collection<T> data) {
return of(HttpStatus.OK.value(), msg, data);
}
public static <T> ResponseList<T> ofSuccess(String msg, Page<T> data) {
return of(HttpStatus.OK.value(), msg, data);
}
/**
@ -69,7 +87,7 @@ public class ResponseEntity<T> {
}
}
QueryPage(T[] data) {
QueryPage(T... data) {
//筛掉所有null
this.records = Arrays.stream(data).filter(Objects::nonNull).collect(Collectors.toList());
this.total = (long) records.size();

View File

@ -0,0 +1,103 @@
package cn.edu.hfut.rmdjzz.projectmanagement.utils.response;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.core.serializer.support.SerializationFailedException;
import org.springframework.http.HttpStatus;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* datajsonofofSuccessputputIgnoreNullMap
*
* @author
* @since 2022/6/28 21:29
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Data
public class ResponseMap {
private Integer code;
private String msg;
private Map<String, Object> data;
/**
*
*
* @param code
* @param msg
* @param data CollectionPage
*/
public static ResponseMap of(Integer code, String msg, Object data) {
if (data == null) {
return new ResponseMap(code, msg, new HashMap<>());
}
if (data instanceof Collection<?> || data instanceof Page<?>) {
throw new SerializationFailedException("在使用集合类时请使用ResponseList而不是ResponseMap");
}
if (data instanceof Map<?, ?> mapData) {
return of(code, msg, mapData, true);
}
return new ResponseMap(code, msg, BeanUtils.beanToMap(data, false));
}
/**
* @param putNulls falsemapnull
*/
public static ResponseMap of(Integer code, String msg, Map<?, ?> data, boolean putNulls) {
ResponseMap responseMap = new ResponseMap(code, msg, new HashMap<>());
if (data == null) {
return responseMap;
}
data.forEach((key, value) -> {
if (putNulls || value != null) {
responseMap.put(String.valueOf(key), value);
}
});
return responseMap;
}
/**
*
*/
public static ResponseMap of(Integer code, String msg) {
return new ResponseMap(code, msg, new HashMap<>());
}
/**
*
*/
public static ResponseMap ofSuccess(String msg, Object data) {
return of(HttpStatus.OK.value(), msg, data);
}
public static ResponseMap ofSuccess(String msg, Map<?, ?> data, boolean putNulls) {
return of(HttpStatus.OK.value(), msg, data, putNulls);
}
public static ResponseMap ofSuccess(String msg) {
return of(HttpStatus.OK.value(), msg);
}
/**
* Map
*/
public ResponseMap put(String key, Object value) {
data.put(key, value);
return this;
}
/**
* valuenullputmap
*/
public ResponseMap putIgnoreNull(String key, Object value) {
if (value != null) {
data.put(key, value);
}
return this;
}
}

View File

@ -1,6 +1,9 @@
package cn.edu.hfut.rmdjzz.projectmanagement;
import cn.edu.hfut.rmdjzz.projectmanagement.service.impl.StaffServiceImpl;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Staff;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.response.ResponseMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
@ -16,13 +19,17 @@ import javax.annotation.Resource;
public class MybatisPlusTests {
@Resource
StaffServiceImpl staffService;
private IStaffService staffService;
@Resource
private ObjectMapper objectMapper;
@SneakyThrows
@Test
public void test() {
System.out.println(objectMapper.writeValueAsString(staffService.list()));
System.out.println(objectMapper.writeValueAsString(
ResponseMap.ofSuccess("ok",
staffService.getOne(Wrappers.<Staff>query().last("limit 1"))
)
));
}
}