diff --git a/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java b/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java index 37739404..a218e4f7 100644 --- a/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java +++ b/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java @@ -1,100 +1,192 @@ package org.opsli.api.base.result; +import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AccessLevel; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; import org.springframework.http.HttpStatus; - import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - /** - * Web统一返回参数 + * API 统一返回参数 * * @date 2020年5月15日10:40:54 * @author Parker - * + * + * 在 Feign 的调用过程中,无法直接序列化数据 + * + * 所以要加上 泛型对象 @JsonProperty ,否者返回则为一个null + * */ +@Data @ApiModel(value="视图层返回Api对象", description="视图层返回Api对象 success:成功状态 code:编号 msg:信息 datatime:时间戳") -public class ResultVo extends HashMap implements Serializable { - - - public ResultVo(){ - this.put("success", true); - this.put("code", HttpStatus.OK.value()); - this.put("msg", "操作成功"); - this.put("datatime", System.currentTimeMillis()); - } +public class ResultVo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** 成功状态 */ + @ApiModelProperty(value = "成功状态") + private boolean success; + + /** 消息 */ + @ApiModelProperty(value = "消息") + private String msg; + + /** 状态码 */ + @ApiModelProperty(value = "状态码") + private Integer code; + + /** 时间戳 */ + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) + @ApiModelProperty(value = "时间戳") + private long timestamp; + + /** 数据对象 */ + @ApiModelProperty(value = "数据") + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) + @JsonProperty("data") + private T data; - /** get/set - * msg : 信息 - * code : 编码 - * success : 是否成功状态 - * */ - public String getMsg() { - return (String)this.get("msg"); - } - - public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg - this.put("msg", msg); - } - public int getCode() { - return (int)this.get("code"); + /** + * 设置值 + * @param data 数据 + * @return ResultVo + */ + public ResultVo put(T data) { + this.data = data; + return this; } - public void setCode(int code) { - this.put("code", code); + /** + * 获得值 + * @return T + */ + public T get(){ + return this.data; } - public boolean isSuccess() { - return (boolean)this.get("success"); + /** + * 转化成Json字符串 + * @return String + */ + public String toJsonStr(){ + return JSONObject.toJSONString(this); } - - public void setSuccess(boolean success) { - this.put("success", success); + + // =========================================== + + /** + * 构造函数 + */ + public ResultVo() { + // 初始化值 + this.success = true; + this.msg = "操作成功!"; + this.code = HttpStatus.OK.value(); + this.timestamp = System.currentTimeMillis(); } - // ------------------------------------------- + // ================================== 静态方法 =================================== + /** + * 返回成功状态 + * @return ResultVo + */ @JsonIgnore//返回对象时忽略此属性 - public static ResultVo success(String msg) { - ResultVo j = new ResultVo(); - j.setMsg(msg); - return j; + public static ResultVo success() { + return new ResultVo<>(); } + + /** + * 返回成功状态 + * @param msg 返回信息 + * @return ResultVo + */ @JsonIgnore//返回对象时忽略此属性 - public static ResultVo error(String msg) { - ResultVo j = new ResultVo(); - j.setSuccess(false); - j.setMsg(msg); - return j; + public static ResultVo success(String msg) { + ResultVo ret = new ResultVo<>(); + ret.setMsg(msg); + return ret; } + /** + * 返回成功状态 + * @param data 返回数据 + * @param 泛型 + * @return ResultVo + */ @JsonIgnore//返回对象时忽略此属性 - public static ResultVo success(Map map) { - ResultVo restResponse = new ResultVo(); - restResponse.putAll(map); - return restResponse; + public static ResultVo success(T data) { + ResultVo ret = new ResultVo<>(); + ret.put(data); + return ret; } + /** + * 返回成功状态 + * @param msg 返回信息 + * @param data 返回数据 + * @param 泛型 + * @return ResultVo + */ @JsonIgnore//返回对象时忽略此属性 - public static ResultVo success() { - return new ResultVo(); + public static ResultVo success(String msg, T data) { + ResultVo ret = new ResultVo<>(); + ret.put(data); + return ret; } - - - @Override - public ResultVo put(String key, Object value) { - super.put(key, value); - return this; + + + /** + * 返回错误状态 + * @param msg 返回信息 + * @return ResultVo + */ + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo error(String msg) { + ResultVo ret = new ResultVo<>(); + ret.setMsg(msg); + ret.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value()); + return ret; } - - public ResultVo putMap(Map m) { - super.putAll(m); - return this; + + /** + * 返回错误状态 + * @param code 错误编号 + * @param msg 返回信息 + * @return ResultVo + */ + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo error(int code, String msg) { + ResultVo ret = new ResultVo<>(); + ret.setMsg(msg); + ret.setCode(code); + return ret; + } + + /** + * 返回成功状态 + * @param code 错误编号 + * @param data 返回数据 + * @param 泛型 + * @return ResultVo + */ + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo error(int code, String msg, T data) { + ResultVo ret = new ResultVo<>(); + ret.setMsg(msg); + ret.setCode(code); + ret.put(data); + return ret; } } \ No newline at end of file diff --git a/opsli-api/src/main/java/org/opsli/api/web/test/TestApi.java b/opsli-api/src/main/java/org/opsli/api/web/test/TestApi.java index bda70223..97ccd60f 100644 --- a/opsli-api/src/main/java/org/opsli/api/web/test/TestApi.java +++ b/opsli-api/src/main/java/org/opsli/api/web/test/TestApi.java @@ -25,7 +25,7 @@ public interface TestApi { @ApiOperation(value = "发送邮件", notes = "发送邮件") @GetMapping("/sendMail") - ResultVo sendMail(); + ResultVo sendMail(); /** @@ -34,7 +34,7 @@ public interface TestApi { */ @ApiOperation(value = "发送 Redis 订阅消息", notes = "发送 Redis 订阅消息") @GetMapping("/sendMsg") - ResultVo sendMsg(); + ResultVo sendMsg(); /** @@ -43,7 +43,7 @@ public interface TestApi { */ @ApiOperation(value = "发送 Redis 测试", notes = "发送 Redis 测试") @GetMapping("/redisTest") - ResultVo redisTest(); + ResultVo redisTest(); /** @@ -52,7 +52,7 @@ public interface TestApi { */ @ApiOperation(value = "发起 Redis 分布式锁", notes = "发起 Redis 分布式锁") @GetMapping("/testLock") - ResultVo testLock(); + ResultVo testLock(); /** * 新增数据 @@ -60,7 +60,7 @@ public interface TestApi { */ @ApiOperation(value = "新增数据", notes = "新增数据") @GetMapping("/insert") - ResultVo insert(TestModel entity); + ResultVo insert(TestModel entity); /** * 修改数据 @@ -68,7 +68,7 @@ public interface TestApi { */ @ApiOperation(value = "修改数据", notes = "修改数据") @GetMapping("/update") - ResultVo update(TestModel entity); + ResultVo update(TestModel entity); /** @@ -77,7 +77,7 @@ public interface TestApi { */ @ApiOperation(value = "查看对象", notes = "查看对象") @GetMapping("/get") - ResultVo get(TestModel entity); + ResultVo get(TestModel entity); /** @@ -86,7 +86,7 @@ public interface TestApi { */ @ApiOperation(value = "删除对象", notes = "删除对象") @GetMapping("/del") - ResultVo del(String id); + ResultVo del(String id); /** @@ -95,7 +95,7 @@ public interface TestApi { */ @ApiOperation(value = "删除全部对象", notes = "删除全部对象") @GetMapping("/delAll") - ResultVo delAll(); + ResultVo delAll(); } diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java new file mode 100644 index 00000000..28faf5e2 --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java @@ -0,0 +1,97 @@ +package org.opsli.common.api; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.springframework.http.HttpStatus; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + + +/** + * Web统一返回参数 + * + * @date 2020年5月15日10:40:54 + * @author Parker + * + */ +public class ResultVo extends HashMap implements Serializable { + + + public ResultVo(){ + this.put("success", true); + this.put("code", HttpStatus.OK.value()); + this.put("msg", "操作成功"); + this.put("datatime", System.currentTimeMillis()); + } + + /** get/set + * msg : 信息 + * code : 编码 + * success : 是否成功状态 + * */ + public String getMsg() { + return (String)this.get("msg"); + } + + public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg + this.put("msg", msg); + } + + public int getCode() { + return (int)this.get("code"); + } + + public void setCode(int code) { + this.put("code", code); + } + + public boolean isSuccess() { + return (boolean)this.get("success"); + } + + public void setSuccess(boolean success) { + this.put("success", success); + } + + // ------------------------------------------- + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success(String msg) { + ResultVo j = new ResultVo(); + j.setMsg(msg); + return j; + } + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo error(String msg) { + ResultVo j = new ResultVo(); + j.setSuccess(false); + j.setMsg(msg); + return j; + } + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success(Map map) { + ResultVo restResponse = new ResultVo(); + restResponse.putAll(map); + return restResponse; + } + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success() { + return new ResultVo(); + } + + + @Override + public ResultVo put(String key, Object value) { + super.put(key, value); + return this; + } + + public ResultVo putMap(Map m) { + super.putAll(m); + return this; + } + +} \ No newline at end of file diff --git a/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/service/impl/TestServiceImpl.java b/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/service/impl/TestServiceImpl.java index 5b92008c..e0606353 100644 --- a/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/service/impl/TestServiceImpl.java +++ b/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/service/impl/TestServiceImpl.java @@ -30,7 +30,7 @@ import java.util.Collection; */ @Service // 开启热数据标示 不加不生效 -//@EnableHotData +@EnableHotData public class TestServiceImpl extends CrudServiceImpl implements ITestService { @Autowired(required = false) diff --git a/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/web/TestRestRestController.java b/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/web/TestRestRestController.java index 6ae819f3..8d42b768 100644 --- a/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/web/TestRestRestController.java +++ b/opsli-modulars/opsli-modulars-test/src/main/java/org/opsli/modulars/test/web/TestRestRestController.java @@ -51,7 +51,7 @@ public class TestRestRestController extends BaseRestController sendMail(){ MailModel mailModel = new MailModel(); mailModel.setTo("meet.carina@foxmail.com"); mailModel.setSubject("测试邮件功能"); @@ -67,7 +67,7 @@ public class TestRestRestController extends BaseRestController sendMsg(){ BaseSubMessage msg = DictMsgFactory.createMsg("test", "aaa", 123213, CacheType.UPDATE); @@ -85,13 +85,11 @@ public class TestRestRestController extends BaseRestController redisTest(){ boolean ret = redisPlugin.put("opsli:test", "12315"); if(ret){ Object o = redisPlugin.get("opsli:test"); - ResultVo resultVo = new ResultVo(); - resultVo.put("data",o); - return resultVo; + return ResultVo.success(o); } return ResultVo.error("发送订阅消息失败!!!!!!"); } @@ -103,7 +101,7 @@ public class TestRestRestController extends BaseRestController testLock(){ // 锁凭证 redisLock 贯穿全程 RedisLock redisLock = new RedisLock(); @@ -114,8 +112,7 @@ public class TestRestRestController extends BaseRestController error = ResultVo.error(500,"获得锁失败!!!!!!",redisLock); return error; } @@ -123,8 +120,8 @@ public class TestRestRestController extends BaseRestController success = ResultVo.success("获得锁成功!!!!!!",redisLock); + success.put(redisLock); return success; } @@ -134,7 +131,7 @@ public class TestRestRestController extends BaseRestController insert(TestModel model){ // 转化对象 处理 ApiModel 与 本地对象 model.setName("测试名称"+random.nextInt()); @@ -143,10 +140,7 @@ public class TestRestRestController extends BaseRestController update(TestModel model){ // 转化对象 处理 ApiModel 与 本地对象 if(StringUtils.isEmpty(model.getId())){ @@ -168,11 +162,7 @@ public class TestRestRestController extends BaseRestController get(TestModel model){ + return ResultVo.success(model); } @@ -196,7 +183,7 @@ public class TestRestRestController extends BaseRestController del(String id){ TestEntity testEntity1 = new TestEntity(); testEntity1.setId(id); @@ -211,13 +198,9 @@ public class TestRestRestController extends BaseRestController delAll(){ //IService. @@ -241,12 +224,7 @@ public class TestRestRestController extends BaseRestController insert(){ + for (int i = 0; i < 999; i++) { + testApi.insert(new TestModel()); + } return testApi.insert(new TestModel()); }