From 779aadfa90600a7453d0b9bf1cf1db6dcf92d2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 23 Mar 2023 18:12:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=8E=B7=E5=8F=96redis=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constant/CommonConstant.java | 20 +++ .../controller/RedisController.java | 26 ++-- .../kwan/springbootkwan/entity/Result.java | 126 ++++++++++++++++++ src/main/resources/application.yaml | 22 +-- 4 files changed, 170 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/Result.java diff --git a/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java b/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java new file mode 100644 index 0000000..5a94b57 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java @@ -0,0 +1,20 @@ +package com.kwan.springbootkwan.constant; + +public class CommonConstant { + /** + * 成功 + */ + public static final Integer SC_OK_200 = 200; + /** + * 服务器错误 + */ + public static final Integer SC_INTERNAL_SERVER_ERROR_500 = 500; + /** + * 未认证 + */ + public static final int SC_JEECG_NO_AUTHZ = 401; + /** + * redis前缀 + */ + public static final String PREFIX_REDIS_KEY = "test:"; +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java index d9b5ea8..856dc5e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java @@ -1,7 +1,9 @@ package com.kwan.springbootkwan.controller; +import com.kwan.springbootkwan.entity.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,35 +12,33 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; +import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY; + +@Slf4j @Api(value = "redis测试信息", tags = "RedisController") @RestController @RequestMapping("/test") public class RedisController { - public static final String REDIS_KEY = "test:_w"; - @Resource private RedisTemplate redisTemplate; - @ApiOperation(value = "redis添加数据", notes = "redis添加数据") @GetMapping("/add") - public void add() { - //当前时间时间戳 - String nowStr = String.valueOf(System.currentTimeMillis()); - //设置redis中的key值 - String redisKey = REDIS_KEY.concat(nowStr); + public Result add(String key, String value) { //向对应的key值添加数据 - redisTemplate.opsForValue().set(REDIS_KEY, "测试向redis中添加数据"); + redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value); //设置过期时间 - redisTemplate.expire(REDIS_KEY, 2000000, TimeUnit.SECONDS); + redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS); + return Result.ok(); } @ApiOperation(value = "redis获取数据", notes = "redis获取数据") @GetMapping("/get") - public void get() { + public Result get(String key) { //获取redis中的数据 - String str = (String) redisTemplate.opsForValue().get(REDIS_KEY); - System.out.println("获取redis中的数据-->" + str); + String value = (String) redisTemplate.opsForValue().get(PREFIX_REDIS_KEY + key); + log.info("获取redis中的数据-->{}", value); + return Result.ok(value); } } diff --git a/src/main/java/com/kwan/springbootkwan/entity/Result.java b/src/main/java/com/kwan/springbootkwan/entity/Result.java new file mode 100644 index 0000000..b2b91fc --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/Result.java @@ -0,0 +1,126 @@ +package com.kwan.springbootkwan.entity; + +import com.kwan.springbootkwan.constant.CommonConstant; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; + +/** + * 接口返回数据格式 + * + * @author scott + * @email jeecgos@163.com + * @date 2019年1月19日 + */ +@Data +@ApiModel(value = "接口返回对象", description = "接口返回对象") +public class Result implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 成功标志 + */ + @ApiModelProperty(value = "成功标志") + private boolean success = true; + + /** + * 返回处理消息 + */ + @ApiModelProperty(value = "返回处理消息") + private String message = "操作成功!"; + + /** + * 返回代码 + */ + @ApiModelProperty(value = "返回代码") + private Integer code = 0; + + /** + * 返回数据对象 data + */ + @ApiModelProperty(value = "返回数据对象") + private T result; + + /** + * 时间戳 + */ + @ApiModelProperty(value = "时间戳") + private long timestamp = System.currentTimeMillis(); + + public Result() { + + } + + public Result success(String message) { + this.message = message; + this.code = CommonConstant.SC_OK_200; + this.success = true; + return this; + } + + public Result good(T t) { + this.setResult(t); + this.code = CommonConstant.SC_OK_200; + this.success = true; + return this; + } + + public Result good() { + this.code = CommonConstant.SC_OK_200; + this.success = true; + this.setMessage("成功"); + return this; + } + + public Result fail(String msg) { + this.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500); + this.setMessage(msg); + this.setSuccess(false); + return this; + } + + public static Result ok() { + Result r = new Result<>(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setMessage("成功"); + return r; + } + + public static Result ok(Object data) { + Result r = new Result<>(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setResult(data); + return r; + } + + public static Result error(String msg) { + return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg); + } + + public static Result error(int code, String msg) { + Result r = new Result(); + r.setCode(code); + r.setMessage(msg); + r.setSuccess(false); + return r; + } + + public Result error500(String message) { + this.message = message; + this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500; + this.success = false; + return this; + } + + /** + * 无权限访问返回结果 + */ + public static Result noauth(String msg) { + return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 129ae9c..df2fa5a 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,5 +1,5 @@ server: - port: 8761 + port: 8888 servlet: encoding: force: true @@ -38,7 +38,7 @@ spring: database: 0 # Redis数据库索引(默认为0) host: 120.79.36.53 #Redis服务器地址 port: 6379 # Redis服务器连接端口 - password: # Redis服务器连接密码(默认为空) + password: 123456 # Redis服务器连接密码(默认为空) jedis: pool: max-active: 200 # 连接池最大连接数(使用负值表示没有限制) @@ -66,15 +66,15 @@ spring: password: 15671628341Qwe. -seata: - application-id: spring-boot-name - tx-service-group: my-tx-group - service: - vgroup-mapping: - my-tx-group: seata-server - grouplist: - seata-server: 127.0.0.1:8091 - enabled: true +#seata: +# application-id: spring-boot-name +# tx-service-group: my-tx-group +# service: +# vgroup-mapping: +# my-tx-group: seata-server +# grouplist: +# seata-server: 127.0.0.1:8091 +# enabled: true -- GitLab