优化统一返回API

v1.4.1
Parker 5 years ago
parent 2b31db2988
commit 722e69c76c

@ -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 202051510:40:54
* @author Parker
*
* Feign
*
* @JsonProperty null
*
*/
@Data
@ApiModel(value="视图层返回Api对象",
description="视图层返回Api对象 success:成功状态 code:编号 msg:信息 datatime:时间戳")
public class ResultVo extends HashMap<String,Object> implements Serializable {
public class ResultVo<T> implements Serializable {
private static final long serialVersionUID = 1L;
public ResultVo(){
this.put("success", true);
this.put("code", HttpStatus.OK.value());
this.put("msg", "操作成功");
this.put("datatime", System.currentTimeMillis());
}
/** 成功状态 */
@ApiModelProperty(value = "成功状态")
private boolean success;
/** get/set
* msg :
* code :
* success :
* */
public String getMsg() {
return (String)this.get("msg");
}
/** 消息 */
@ApiModelProperty(value = "消息")
private String msg;
public void setMsg(String msg) {//向json中添加属性在js中访问请调用data.msg
this.put("msg", msg);
}
/** 状态码 */
@ApiModelProperty(value = "状态码")
private Integer code;
/** 时间戳 */
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@ApiModelProperty(value = "时间戳")
private long timestamp;
public int getCode() {
return (int)this.get("code");
/** 数据对象 */
@ApiModelProperty(value = "数据")
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@JsonProperty("data")
private T data;
/**
*
* @param data
* @return ResultVo<T>
*/
public ResultVo<T> 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<Object>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo success(String msg) {
ResultVo j = new ResultVo();
j.setMsg(msg);
return j;
public static ResultVo<Object> success() {
return new ResultVo<>();
}
/**
*
* @param msg
* @return ResultVo<Object>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo error(String msg) {
ResultVo j = new ResultVo();
j.setSuccess(false);
j.setMsg(msg);
return j;
public static ResultVo<Object> success(String msg) {
ResultVo<Object> ret = new ResultVo<>();
ret.setMsg(msg);
return ret;
}
/**
*
* @param data
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo success(Map<String, Object> map) {
ResultVo restResponse = new ResultVo();
restResponse.putAll(map);
return restResponse;
public static <T> ResultVo<T> success(T data) {
ResultVo<T> ret = new ResultVo<>();
ret.put(data);
return ret;
}
/**
*
* @param msg
* @param data
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo success() {
return new ResultVo();
public static <T> ResultVo<T> success(String msg, T data) {
ResultVo<T> 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<Object>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo<Object> error(String msg) {
ResultVo<Object> 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<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static ResultVo<Object> error(int code, String msg) {
ResultVo<Object> ret = new ResultVo<>();
ret.setMsg(msg);
ret.setCode(code);
return ret;
}
/**
*
* @param code
* @param data
* @param <T>
* @return ResultVo<T>
*/
@JsonIgnore//返回对象时忽略此属性
public static <T> ResultVo<T> error(int code, String msg, T data) {
ResultVo<T> ret = new ResultVo<>();
ret.setMsg(msg);
ret.setCode(code);
ret.put(data);
return ret;
}
}

@ -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<TestModel> insert(TestModel entity);
/**
*
@ -68,7 +68,7 @@ public interface TestApi {
*/
@ApiOperation(value = "修改数据", notes = "修改数据")
@GetMapping("/update")
ResultVo update(TestModel entity);
ResultVo<TestModel> update(TestModel entity);
/**
@ -77,7 +77,7 @@ public interface TestApi {
*/
@ApiOperation(value = "查看对象", notes = "查看对象")
@GetMapping("/get")
ResultVo get(TestModel entity);
ResultVo<TestModel> 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();
}

@ -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 202051510:40:54
* @author Parker
*
*/
public class ResultVo extends HashMap<String,Object> 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<String, Object> 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;
}
}

@ -30,7 +30,7 @@ import java.util.Collection;
*/
@Service
// 开启热数据标示 不加不生效
//@EnableHotData
@EnableHotData
public class TestServiceImpl extends CrudServiceImpl<TestMapper, TestModel, TestEntity> implements ITestService {
@Autowired(required = false)

@ -51,7 +51,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
@ApiOperation(value = "发送邮件", notes = "发送邮件")
@Override
public ResultVo sendMail(){
public ResultVo<?> sendMail(){
MailModel mailModel = new MailModel();
mailModel.setTo("meet.carina@foxmail.com");
mailModel.setSubject("测试邮件功能");
@ -67,7 +67,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "发送 Redis 订阅消息", notes = "发送 Redis 订阅消息")
@Override
public ResultVo sendMsg(){
public ResultVo<?> sendMsg(){
BaseSubMessage msg = DictMsgFactory.createMsg("test", "aaa", 123213, CacheType.UPDATE);
@ -85,13 +85,11 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "发送 Redis 测试", notes = "发送 Redis 测试")
@Override
public ResultVo redisTest(){
public ResultVo<?> 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<TestModel, TestEn
*/
@ApiOperation(value = "发起 Redis 分布式锁", notes = "发起 Redis 分布式锁")
@Override
public ResultVo testLock(){
public ResultVo<RedisLock> testLock(){
// 锁凭证 redisLock 贯穿全程
RedisLock redisLock = new RedisLock();
@ -114,8 +112,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
redisLock = redisLockPlugins.tryLock(redisLock);
if(redisLock == null){
ResultVo error = ResultVo.error("获得锁失败!!!!!!");
error.put("redisLock",redisLock);
ResultVo<RedisLock> error = ResultVo.error(500,"获得锁失败!!!!!!",redisLock);
return error;
}
@ -123,8 +120,8 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
ThreadUtil.sleep(60, TimeUnit.SECONDS);
redisLockPlugins.unLock(redisLock);
ResultVo success = ResultVo.success("获得锁成功!!!!!!");
success.put("redisLock",redisLock);
ResultVo<RedisLock> success = ResultVo.success("获得锁成功!!!!!!",redisLock);
success.put(redisLock);
return success;
}
@ -134,7 +131,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "新增数据", notes = "新增数据")
@Override
public ResultVo insert(TestModel model){
public ResultVo<TestModel> insert(TestModel model){
// 转化对象 处理 ApiModel 与 本地对象
model.setName("测试名称"+random.nextInt());
@ -143,10 +140,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
// 调用新增方法
TestModel insert = IService.insert(model);
ResultVo resultVo = new ResultVo();
resultVo.setMsg("新增成功");
resultVo.put("data",insert);
return resultVo;
return ResultVo.success("新增成功",insert);
}
/**
@ -155,7 +149,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "修改数据", notes = "修改数据")
@Override
public ResultVo update(TestModel model){
public ResultVo<TestModel> update(TestModel model){
// 转化对象 处理 ApiModel 与 本地对象
if(StringUtils.isEmpty(model.getId())){
@ -168,11 +162,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
// 不需要做 锁状态处理,需要判断是否成功 能往下走的只能是成功
TestModel update = IService.update(model);
ResultVo resultVo = new ResultVo();
resultVo.setMsg("修改成功");
resultVo.put("data",update);
return resultVo;
return ResultVo.success("修改成功",update);
}
@ -182,11 +172,8 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "查看对象", notes = "查看对象")
@Override
public ResultVo get(TestModel model){
// 转化对象 处理 ApiModel 与 本地对象
ResultVo resultVo = new ResultVo();
resultVo.put("data",model);
return resultVo;
public ResultVo<TestModel> get(TestModel model){
return ResultVo.success(model);
}
@ -196,7 +183,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "删除对象", notes = "删除对象")
@Override
public ResultVo del(String id){
public ResultVo<?> del(String id){
TestEntity testEntity1 = new TestEntity();
testEntity1.setId(id);
@ -211,13 +198,9 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
//count = IService.deleteAll(idList);
IService.deleteAll(ids);
ResultVo resultVo = new ResultVo();
resultVo.put("data",id);
resultVo.setMsg("删除对象成功");
return resultVo;
return ResultVo.error("删除对象成功");
}
@ -227,7 +210,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
*/
@ApiOperation(value = "删除全部对象", notes = "删除全部对象")
@Override
public ResultVo delAll(){
public ResultVo<?> delAll(){
//IService.
@ -241,12 +224,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
//count = IService.deleteAll(idList);
ResultVo resultVo = new ResultVo();
resultVo.setMsg("删除对象成功");
return resultVo;
return ResultVo.error("删除对象成功");
}

@ -22,7 +22,10 @@ public class TestRestRestController2{
TestApi testApi;
@GetMapping("/insert2")
public ResultVo insert(){
public ResultVo<TestModel> insert(){
for (int i = 0; i < 999; i++) {
testApi.insert(new TestModel());
}
return testApi.insert(new TestModel());
}

Loading…
Cancel
Save