0

I'm using Spring Boot 2, Springa Data REST, Spring HATEOAS. I've a particular @RepositoryRestController in which I need to return a paged result of Strings. So I've not objects but a list of String.

This is how my controller looks like:

@RepositoryRestController
@PreAuthorize("isAuthenticated()")
public class FrameController {
     @PostMapping(path = "/frames/{property}/groupBy")
    public ResponseEntity<?> search(@PathVariable("property") String property, @RequestBody(required = true) List<Filter> filters, Pageable pageable, Locale locale,
                                    PersistentEntityResourceAssembler resourceAssembler) {
        Page<String> pageResult = frameService.groupByProperty(property, filters, pageable);

        return ResponseEntity.ok(pagedResourcesAssembler.toResource(pageResult));
    }

The response looks like this:

{
    "_embedded": {
        "strings": [
            {
                "content": "BA0001"
            },
            {
                "content": "BA0002"
            },
            {
                "content": "BA0003"
            },
            {
                "content": "BA0004"
            },
            {
                "content": "BA0005"
            },
            {
                "content": "BA0006"
            },
            {
                "content": "BA0007"
            }
        ]
    },
    "_links": {
        "first": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=0&size=20"
        },
        "self": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=0&size=20"
        },
        "next": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=1&size=20"
        },
        "last": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=585&size=20"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 11717,
        "totalPages": 586,
        "number": 0
    }
}

The response is quite ugly because each string looks like an object.

I would like to have a response like this:

{
    "_embedded": {
        "strings": [
           "BA0001",
           "BA0002",
           "BA0003",
           "BA0004",
           "BA0005",
           "BA0006",
           "BA0007"
        ]
    },
    "_links": {
        "first": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=0&size=20"
        },
        "self": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=0&size=20"
        },
        "next": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=1&size=20"
        },
        "last": {
            "href": "http://localhost:8082/api/v1/frames/model/groupBy?page=585&size=20"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 11717,
        "totalPages": 586,
        "number": 0
    }
}

I tried to create my own ResourceAssembler but unfortunately PagedResourcesAssembler applies the resourceAssembler to each item (String) and not to the entire collection.

Is there a way to reach my goal using Spring stuff?

drenda
  • 5,846
  • 11
  • 68
  • 141

0 Answers0