You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Redis OM Java时遇ERR unknown command 'JSON.SET'错误求助

解决Redis OM Java调用JSON.SET报错的问题

问题描述

使用Redis OM与Java开发时遇到如下错误:

Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'JSON.SET'

环境与代码信息

  • 环境版本:Java 17、Spring Boot 2.7.5、Redis镜像redis/redis-stack:7.2.0-v1
  • 核心代码:
@SpringBootApplication
@Configuration
@EnableRedisDocumentRepositories(basePackages = "jphaugla.redisom.com.romsdocuments.*")
public class RomsDocumentsApplication {

    @Autowired
    CompanyRepository companyRepo;

    @Bean
    CommandLineRunner loadTestData() {
        return args -> {
            Company redis = Company.of("https://redis.com", new Point(-122.066540, 37.377690), 526,
                    2011, "Redis", "Salavatore Sanfilippo");
            redis.setTags(Set.of("fast", "scalable", "reliable"));

            companyRepo.save(redis);
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(RomsDocumentsApplication.class, args);
    }
}
  • 依赖配置:
<dependency>
    <groupId>com.redis.om</groupId>
    <artifactId>redis-om-spring</artifactId>
    <version>0.6.3</version>
</dependency>
  • 实体类:
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document
public class Company {
  @Id
  private String id;
  @Indexed
  private Set<String> tags = new HashSet<String>();
  @NonNull
  private String url;
  @NonNull
  @Indexed
  private Point location;
  @NonNull
  @Indexed
  private Integer numberOfEmployees;
  @NonNull
  @Indexed
  private Integer yearFounded;
  @NonNull
  @Searchable
  private String name;
  @Searchable
  @NonNull
  private String founder;
  private boolean publiclyListed;
}

补充:已确认Redis安装ReJson模块,Redis-Cli可正常调用JSON.SET。

排查与解决方案

1. 核对Redis连接配置

优先检查application.properties/application.yml中的Redis连接参数是否指向正确的Redis Stack实例:

spring.redis.host=localhost
spring.redis.port=6379
# 若设置了密码需添加
# spring.redis.password=your_password

常见问题:代码可能连接到了本地默认的普通Redis实例(而非Redis Stack),即使Redis Stack容器已启动,也要确认端口、主机地址完全匹配。

2. 验证连接目标实例信息

添加调试代码,确认客户端实际连接的Redis实例是否加载了ReJSON模块:

@Autowired
private RedisConnectionFactory connectionFactory;

@Bean
CommandLineRunner checkRedisInstance() {
    return args -> {
        try (RedisConnection connection = connectionFactory.getConnection()) {
            // 打印Redis版本
            System.out.println("Connected Redis Version: " + connection.info("server").get("redis_version"));
            // 打印已加载模块
            System.out.println("Loaded Modules: " + connection.info("modules"));
        }
    };
}

如果输出中看不到ReJSON模块,说明连接的并非Redis Stack实例,需修正连接配置。

3. 升级Redis OM版本

当前使用的redis-om-spring 0.6.3与Redis Stack 7.2.0-v1可能存在兼容性问题,尝试升级到最新稳定版:

<dependency>
    <groupId>com.redis.om</groupId>
    <artifactId>redis-om-spring</artifactId>
    <version>1.2.0</version> <!-- 替换为最新稳定版本 -->
</dependency>

4. 排查连接池配置干扰

若自定义了Redis连接池配置,检查是否存在错误的主机/端口设置,或是否禁用了JSON相关命令。确保连接池参数未覆盖默认的Redis Stack连接逻辑。

5. 确认Redis Stack运行状态

再次验证Redis Stack容器状态,避免实例意外重启或切换:

# 查看容器运行状态
docker ps
# 进入容器内Redis-Cli,重新测试JSON.SET命令
docker exec -it <container_id> redis-cli

内容的提问来源于stack exchange,提问作者xynb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:57:33