gpt4 book ai didi

Java Spring 5 Get 和 findById mongo MonoOnErrorResume

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:18 26 4
gpt4 key购买 nike

这里是 Spring 新手,尝试通过 findById(id, Object) 在 mongo 数据库中进行 GET http 查询。

但似乎不起作用。我可以POSTPUT,但是当通过ID调用查询时,我得到这个错误MonoOnErrorResume

  • 我正在使用 EmbeddedMongoDB

Controller

public class ContentController {
public static final String CONTENT_V_1_CONT = "/contents/v1/cont/";

private final ContentService contentService;

@Autowired
public ContentController(ContentService contentService) {
this.contentService = contentService;
}

@GetMapping(path = "{id}", produces =
MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Content> getContent(@PathVariable String id) {
System.out.println(contentService.getContent(id)); //
MonoOnErrorResume
return contentService.getContent(id);
}

@PostMapping(path = "", produces =
MediaType.APPLICATION_JSON_UTF8_VALUE, consumes =
MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Content> createContent(@RequestBody Mono<Content> content){
return contentService.createContent(content);
}

服务实现

public final ReactiveMongoOperations reactiveMongoOperations;

@Autowired
public ContentServiceImplementation(ReactiveMongoOperations reactiveMongoOperations) {
this.reactiveMongoOperations = reactiveMongoOperations;
}

@Override
public Mono<Content> getContent(String id) {
return reactiveMongoOperations.findById(id, Content.class);
}

@Override
public Mono<Content> createContent(Mono<Content> contentMono) {
return reactiveMongoOperations.save(contentMono);
}

数据配置不知道有用吗

@Bean
public ReactiveMongoDatabaseFactory mongoDatabaseFactory(MongoClient mongoClient){
return new SimpleReactiveMongoDatabaseFactory(mongoClient, DATABASE_NAME);
}

@Bean
public ReactiveMongoOperations reactiveMongoTemplate(ReactiveMongoDatabaseFactory mongoDatabaseFactory){
return new ReactiveMongoTemplate(mongoDatabaseFactory);
}

请注意我是否遗漏了一些关键信息

最佳答案

你的问题可能来自你的 Controller ,你像这样声明你的路径:

@GetMapping(path = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

因此,除非您的 Controller 类映射末尾有一个 / ,否则您将会遇到问题,因为您的最终 URL 将如下所示:

http://localhost:8080/my/route/get1

而不是:

http://localhost:8080/my/route/get/1

你的@PathVariable看起来也很奇怪,尝试这样做:

@PathVariable("id") String id

确保 Spring 将 {id} 映射到您的 @PathVariable

关于Java Spring 5 Get 和 findById mongo MonoOnErrorResume,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57424890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com