【发布时间】:2017-10-28 05:43:04
【问题描述】:
我尝试创建返回所有产品的休息控制器。我想使用 CompletableFuture 返回产品列表。
我对 Spring 数据有异步请求
@Async
@Query("select product from Product product")
CompletableFuture<List<Product>> findAllAsync();
和控制器
@Async
@RequestMapping(path = "/products", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
CompletableFuture<List<ProductData>> loadAllProducts2(){
return this.products.findAllAsync()
.thenApplyAsync(Collection::stream)
.thenApplyAsync(s -> s.map(Product::data))
.thenApplyAsync(s -> s.collect(Collectors.toList()));
}
ProgramData 是简单的 DTO:
public final class ProductData {
private final String name;
private final String label;
public ProductData(String name, String label) {
this.name = name;
this.label = label;
}
public String getName() {
return this.name;
}
public String getLabel() {
return this.label;
}
}
Spring 什么都不返回,在日志输出中是:
o.s.b.a.e.mvc.EndpointHandlerMapping : 找不到 [/products] 的处理程序方法
有什么想法吗?
【问题讨论】:
-
您不必用
@Controller注释您的控制器,以便Spring 知道它必须拿起它并在上面查找@RequestMapping? -
控制器有@RestController
-
抱歉,我可能看错了方向,但我没有在您的代码中看到该注释
标签: spring hibernate spring-boot spring-data