1、MagCode,错误码枚举类
package com.mgx.common.enums;
import lombok.extern.slf4j.Slf4j;
,Code_402("权限不足,请联系管理员", 402)
,Code_404("找不到相关内容", 404)
,Code_414("请求URI过长", 414)
,Code_415("不支持的媒体类型", 415)
,Code_505("HTTP版本暂不支持", 505)
,Code_2002("传入参数不符合条件", 2002)
,Code_2003("缺少请求参数", 2003)
,Code_2004("请求方式错误", 2004)
,Code_2005("sql语法错误", 2005)
,Code_2006("数据不存在", 2006)
,Code_2013("移出黑名单失败",2013)
,Code_2014("该车辆已在黑名单中",2014)
,Code_2015("请输入正确的手机号",2015)
,Code_2016("不支持的编码格式", 2016)
,Code_2017("不支持的解码格式", 2017)
public static String getMessage(Integer code){
msgCode = Enum.valueOf(MsgCode.class, "Code_" + code);
} catch (IllegalArgumentException e) {
log.error("传入枚举code错误!code:{}",code);
2、统一返回结果类
package com.mgx.common.dto;
import com.mgx.common.enums.MsgCode;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.io.Serializable;
public class Result implements Serializable {
private static final long serialVersionUID = 1L;
@Getter protected boolean success;
@Getter protected Integer code;
@Getter protected String message;
protected String version;
@Getter protected T data;
public static SuccessBuilder success() {
return new SuccessBuilder(Boolean.TRUE, MsgCode.Code_200.getCode(), MsgCode.Code_200.getMsg());
public static FailBuilder failure() {
return new FailBuilder(Boolean.FALSE);
public static class SuccessBuilder {
protected boolean success;
protected String message;
protected String tag = "mgx";
protected String version = "1.0";
protected SuccessBuilder(boolean success, Integer code, String message) {
public SuccessBuilder data(Object data) {
@SuppressWarnings("unchecked")
return new Result<>(success, code, message, tag, version, (T) data);
public static class FailBuilder {
protected boolean success;
protected String message;
protected String tag = "mgx";
protected String version = "1.0";
protected FailBuilder(boolean success) {
public FailBuilder code(Integer code) {
String message = MsgCode.getMessage(code);
public FailBuilder message(String message) {
public FailBuilder data(Object data) {
@SuppressWarnings("unchecked")
return new Result<>(success, code, message, tag, version, (T) data);
3、BooleanResult封装
package com.mgx.common.dto;
import java.io.Serializable;
public class BooleanResult implements Serializable {
public static BooleanResult success(){
BooleanResult booleanResult = new BooleanResult();
booleanResult.setResult(true);
public static BooleanResult fail(){
public static BooleanResult fail(String reason){
BooleanResult booleanResult = new BooleanResult();
booleanResult.setResult(false);
booleanResult.setReason(reason);
4、结构展示
5、类
package com.mgx.controller;
import com.mgx.common.dto.BooleanResult;
import com.mgx.common.dto.Result;
import com.mgx.service.UnifyService;
import com.mgx.vo.param.SaveInfoUserParam;
import com.mgx.vo.result.InfoUserResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@Api(tags = "springboot整合 统一类型结果集")
@RequestMapping("/Unify")
public class UnifyController {
private UnifyService unifyService;
public Result add(@RequestBody SaveInfoUserParam saveInfoUserParam) {
return Result.success().data(unifyService.add(saveInfoUserParam)).build();
public Result detail(@ApiParam("用户信息的ID") @Param("id") Long id) {
return Result.success().data(unifyService.detail(id)).build();
@DeleteMapping("/delete")
public Result delete(@ApiParam("用户信息的ID") @Param(value = "id") Long id){
return Result.success().data(unifyService.delete(id)).build();
public Result update(@RequestBody SaveInfoUserParam saveInfoUserParam){
return Result.success().data(unifyService.update(saveInfoUserParam)).build();
import com.mgx.common.dto.BooleanResult;
import com.mgx.vo.param.SaveInfoUserParam;
import com.mgx.vo.result.InfoUserResult;
public interface UnifyService {
BooleanResult add(SaveInfoUserParam saveInfoUserParam);
InfoUserResult detail(Long id);
BooleanResult delete(Long id);
BooleanResult update(SaveInfoUserParam saveInfoUserParam);
package com.mgx.service.impl;
import com.mgx.common.dto.BooleanResult;
import com.mgx.entity.InfoUser;
import com.mgx.mapper.InfoUserMapper;
import com.mgx.service.UnifyService;
import com.mgx.utils.BeanUtil;
import com.mgx.vo.param.SaveInfoUserParam;
import com.mgx.vo.result.InfoUserResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;
public class UnifyServiceImpl implements UnifyService {
private InfoUserMapper infoUserMapper;
public BooleanResult add(SaveInfoUserParam saveInfoUserParam) {
InfoUser infoUser = BeanUtil.map(saveInfoUserParam,InfoUser.class);
int addRow = infoUserMapper.insert(infoUser);
return BooleanResult.fail();
return BooleanResult.success();
public InfoUserResult detail(Long id) {
InfoUser infoUser = infoUserMapper.selectByPrimaryKey(id);
if (Objects.isNull(infoUser)){
throw new RuntimeException("数据不存在");
return BeanUtil.map(infoUser,InfoUserResult.class);
public BooleanResult delete(Long id) {
InfoUser infoUser = infoUserMapper.selectByPrimaryKey(id);
if (Objects.isNull(infoUser)){
throw new RuntimeException("数据不存在");
int deleteRow = infoUserMapper.deleteByPrimaryKey(id);
return BooleanResult.fail();
return BooleanResult.success();
public BooleanResult update(SaveInfoUserParam saveInfoUserParam) {
InfoUser queryInfoUser = infoUserMapper.selectByPrimaryKey(saveInfoUserParam.getId());
if (Objects.isNull(queryInfoUser)){
throw new RuntimeException("数据不存在");
InfoUser infoUser = BeanUtil.map(saveInfoUserParam,InfoUser.class);
int updateRow = infoUserMapper.updateByPrimaryKeySelective(infoUser);
return BooleanResult.fail();
return BooleanResult.success();
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public static T map(Object source, Class target) {
T t = BeanUtils.instantiateClass(target);
BeanUtils.copyProperties(source, t);
public static List mapList(Collection> sourceList, Class target) {
if (sourceList == null) {
List destinationList = new ArrayList<>();
for (Object sourceObject : sourceList) {
T newObj = map(sourceObject, target);
destinationList.add(newObj);
public static void copyProperties(Object source, Object target, String... ignoreProperties) {
if (null != source && null != target) {
BeanUtils.copyProperties(source, target, ignoreProperties);
public static T convert(Object source, Class targetClass) {
T result = targetClass.newInstance();
copyProperties(source, result);
} catch (IllegalAccessException | BeansException | InstantiationException var3) {
throw new RuntimeException(var3);
public static void copyProperties(Object source, Object target) throws BeansException {
BeanUtils.copyProperties(source, target);
if (target instanceof ConversionCustomizble) {
((ConversionCustomizble) target).convertOthers(source);
public interface ConversionCustomizble {
void convertOthers(Object var1);
public static Map beanToMap(Object beanObj) {
if (Objects.isNull(beanObj)) {
Map map = new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(beanObj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
Method getter = property.getReadMethod();
Object value = Objects.isNull(getter) ? null : getter.invoke(beanObj);
throw new RuntimeException(ex);
public static T mapToBean(Map map, Class beanClass) {
if (MapUtils.isEmpty(map)) {
T t = beanClass.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (Objects.nonNull(setter)) {
setter.invoke(t, map.get(property.getName()));
throw new RuntimeException(ex);
6、测试