这里是春季新手,尝试通过 findById(id,Object )在mongo数据库中进行 GET http查询。
但是它似乎没有用。我可以 POST 和 PUT ,但是通过ID调用查询时,我会得到此错误 MonoOnErrorResume
控制器
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);
}
Lmk,如果我缺少一些关键信息
答案 0 :(得分:0)
您的问题可能来自您的控制器,您这样声明路径:
@GetMapping(path = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
因此,除非在控制器类映射的末尾有一个/
,否则您将遇到问题,因为您的最终URL如下所示:
http://localhost:8080/my/route/get1
而不是:
http://localhost:8080/my/route/get/1
您的@PathVariable
看起来也很奇怪,请尝试这样做:
@PathVariable("id") String id
确保Spring将{id}
映射到您的@PathVariable