我正在关注这个问题的第一个答案: Spring MVC 3: return a Spring-Data Page as JSON
我Cannot infer type arguments for ResponseEntity<>
在以下代码块中遇到编译错误:
@GetMapping("all")
@RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
public HttpEntity<PagedResources<SearchDto>> get(@PageableDefault Pageable p, PagedResourcesAssembler<SearchDto> assembler) {
Page<SearchDto> results = searchService.findAll(p);
return new ResponseEntity<>(assembler.toResource(results), HttpStatus.OK);
}
当我第一次尝试使用此代码时,我缺少依赖项,所以我添加了:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
我正在使用快照,所以我没有包含版本。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.BUILD-SNAPSHOT</version>
</parent>
我试图将问题行更改为:
return new ResponseEntity<PagedResources<SearchDto>>(assembler.toResource(results), HttpStatus.OK);
而是得到错误The constructor ResponseEntity<PagedResources<SearchDto>>(PagedResources<Resource<SearchDto>>, HttpStatus) is undefined
。这附带了以下建议:
Remove arguments to match ResponseEntity<PagedResources<SearchDto>>( HttpStatus)
这根本不会返回我的搜索结果。Cast argument 1 into MultiValueMap<String,String>
. 我查看了 PagedResources 的父类,它无法转换为 MultiValueMap。此外,如果我的数字以 JSON 中的字符串形式出现,那将是一个问题。
所以我希望能够返回一个带有页面元数据和结果集的 JSON。感谢您的阅读。