【问题标题】:mongodb reactive spring boot DBRef resolution is not supported不支持mongodb响应式spring boot DBRef解析
【发布时间】:2022-06-20 16:16:56
【问题描述】:

我写了这个程序spring boot mongodb响应

@SpringBootApplication
public class ReactitveMongoDbApplication {

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

    @Bean
    CommandLineRunner ok(SoscieteRepository sos, TransactionRepository trs) {
        return a -> {
            List<Sosciete> ls = List.of(new Sosciete("SG", "sosciete general", 1235.22),
                    new Sosciete("AB", "AIR BOLL", 478.36), new Sosciete("TO", "TOYOTA", 458.24));
            trs.deleteAll().subscribe(null, null, () -> {
                sos.deleteAll().subscribe(null, null, () -> {
                    ls.forEach(t -> sos.save(t).subscribe(so -> {
                        System.out.println(so);
                        for (int i = 0; i < 10; i++) {
                            Transaction tr = new Transaction();
                            tr.setDate(Instant.now());
                            tr.setSosciete(so);
                            double x = 1 + ((Math.random() * 12) - 6) / 100;
                            tr.setPrice(so.getPrice() * x);
                            trs.save(tr).subscribe(ts -> {
                                System.out.println(ts);
                            });
                        }
                    }));
                });
            });
            System.out.println("done !");
        };
    }
}

interface SoscieteRepository extends ReactiveMongoRepository<Sosciete, String> {
}

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
class Sosciete {
    @Id
    private String id;
    private String name;
    private double price;
}

interface TransactionRepository extends ReactiveMongoRepository<Transaction, String> {
}

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
class Transaction {
    @Id
    private String id;
    private Instant date;
    private double price;
    @DBRef(db = "sosciete", lazy = true)
    private Sosciete sosciete;
}

@RestController
class ReactiveRestController {
    @Autowired
    private SoscieteRepository sos;
    @Autowired
    private TransactionRepository trans;

    @GetMapping(value = "/soscietes")
    public Flux<Sosciete> getAllSc() {
        return sos.findAll();
    }

    @GetMapping(value = "/transactions")
    public Flux<Transaction> getAllTr() {
        return trans.findAll();
    }
}

依赖关系:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

aplication.prop 文件:

spring.data.mongodb.database=webflux
spring.data.mongodb.port=27017
server.port=3000

我的代码在此链接中运行良好:

http://localhost:3000/soscietes

但是在这个链接中:

http://localhost:3000/transactions

代码抛出以下错误:

java.lang.UnsupportedOperationException: DBRef resolution is not supported!

这是导致此错误的 DBRef 注释, 它根本不起作用。它引发以下异常。 我们可以添加这样的配置以使其工作吗? 提前谢谢你

【问题讨论】:

    标签: spring mongodb spring-boot reactive dbref


    【解决方案1】:

    我找到了解决方案: 我在 pom.xml 中修改了 spring boot 父版本:

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.6.3</version>
            <relativePath />
        </parent>
    

    到 2.1.2.RELEASE

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    我在 Spring boot 版本中遇到了同样的问题:2.7.0

    我将@DBRef 更改为@DocumentReference 并且它可以工作

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2023-03-17
      • 2019-11-23
      • 2015-03-03
      • 2021-04-03
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多