Spring MVC - Data Page formatted as JSON, getting error: unable to infer type arguments for ResponseEntity<>

355 views Asked by At

I am following the first answer to this question : Spring MVC 3: return a Spring-Data Page as JSON

I am getting compilation error Cannot infer type arguments for ResponseEntity<> in the following block of code :

@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);
}

When I first tried to use this code, I was missing dependencies, so I added:

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
</dependency>

I am using a snapshot, so I didn't include a version.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.BUILD-SNAPSHOT</version>
</parent>

I attempted to change the problem line to :

return new ResponseEntity<PagedResources<SearchDto>>(assembler.toResource(results), HttpStatus.OK);

but instead get the error The constructor ResponseEntity<PagedResources<SearchDto>>(PagedResources<Resource<SearchDto>>, HttpStatus) is undefined. This comes with the suggestions:

  1. Remove arguments to match ResponseEntity<PagedResources<SearchDto>>( HttpStatus) This would not return my search results at all.

  2. Cast argument 1 into MultiValueMap<String,String>. I looked through the parent classes of PagedResources, and it wouldn't be able to cast to a MultiValueMap. Also, it would be a problem if my numbers came in as strings in the JSON.

So I would like to be able to return a JSON with page metadata and the result set. Thank you for reading.

0

There are 0 answers