小编fre*_*ee斩的帖子

使用redis进行Spring启动缓存,密钥有\ xac\xed\x00\x05t\x00\x06

我想使用Spring缓存@Cacheable来管理缓存.真正的缓存是redis.

我的代码是这样的:

@PostMapping("/post")
@CachePut(value = "abc", key = "#key")
public String putInRedis(@RequestParam String key, @RequestParam String value) {
    saveInDB(key, value);

    return value;
}

@GetMapping("/get")
@Cacheable(value = "abc", key = "#key")
public String queryRedis(@RequestParam String key) {

    return findByKey(key);
}
Run Code Online (Sandbox Code Playgroud)

我有帖子请求之后

本地主机:8080 /后键=键和值=价值?

redis服务器看起来很奇怪

127.0.0.1:6379> keys *
1) "abc:\xac\xed\x00\x05t\x00\x03key"
127.0.0.1:6379> GET "abc:\xac\xed\x00\x05t\x00\x03key"
"\xac\xed\x00\x05t\x00\x05value"
Run Code Online (Sandbox Code Playgroud)

Spring缓存

奇怪,Redis的琴键与弹簧-数据jedis

如何将@ Cacheable的Serializer设置为StringRedisTemplate默认值:

public StringRedisTemplate() {
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    setKeySerializer(stringSerializer);
    setValueSerializer(stringSerializer);
    setHashKeySerializer(stringSerializer);
    setHashValueSerializer(stringSerializer);
}
Run Code Online (Sandbox Code Playgroud)

我的application.properties:

spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)

的build.gradle

group 'io.freezhan'
version '1.0-SNAPSHOT'

buildscript { …
Run Code Online (Sandbox Code Playgroud)

java spring caching redis

6
推荐指数
2
解决办法
7942
查看次数

将HashMap的键和值连接到列表

我想将一个HashMap键和值连接到一个带有':'的字符串,并将它们转换为一个列表.

例:

Map<String,String> tags = new HashMap<>();
tags.put("k1","v1");
tags.put("k2","v2");
Run Code Online (Sandbox Code Playgroud)

然后我想得到字符串

K1:V1,K2:V2

我的代码是:

private String transe(Map<String, String> tags) {
    if (tags == null || tags.isEmpty()) {
        return DEFAULT_STATUS_GROUP;
    }
    List<String> tagKVList = new ArrayList<>();
    tags.forEach((key, value) -> tagKVList.add(String.join(":", key, value)));
    tagKVList.sort((tag1, tag2) -> tag1.compareTo(tag2));
    return tagKVList.stream().collect(Collectors.joining(","));
}
Run Code Online (Sandbox Code Playgroud)

如何删除局部变量tagKVList并使代码更清晰?

java string java-8 java-stream

4
推荐指数
1
解决办法
104
查看次数

使用命令行参数覆盖spring-boot中的yml配置不起作用

我有一个春季启动应用程序。我想在执行jar时覆盖在application.yml中配置的某些属性。

我的代码是这样的:

@Service
public class CommandService {

    @Value("${name:defaultName}")
    private String name;

    public void print() {
        System.out.println(name);
    }
}
Run Code Online (Sandbox Code Playgroud)

而且Application.java是

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private CommandService commandService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Override
    public void run(String... args) throws Exception {
        commandService.print();
    }
}
Run Code Online (Sandbox Code Playgroud)

application.yml

名称:nameInApplication

当我执行这样的命令时:

java -jar app.jar --name = nameInCommand

不行

第二个命令也不起作用:

java -Dname = nameInCommand2 -jar app.jar

但是,如果application.yml没有配置名称,则第二个命令将正常运行,而第一个命令也将不起作用。

java configuration command overriding spring-boot

3
推荐指数
1
解决办法
1241
查看次数