Использование PagedResourcesAssembler с содержимым List <String> - PullRequest
0 голосов
/ 28 ноября 2018

Я использую Spring Boot 2, Springa Data REST, Spring HATEOAS.У меня есть конкретный @RepositoryRestController, в котором мне нужно вернуть постраничный результат строк.Так что я имею не объекты, а список String.

Вот так выглядит мой контроллер:

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

Ответ выглядит так:

{
    "_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
    }
}

Ответ довольно уродливый, потому что каждая строка выглядит как объект.

Я хотел бы получить ответ, подобный этому:

{
    "_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
    }
}

Я пытался создать свой собственный ResourceAssembler, нок сожалению, PagedResourcesAssembler применяет resourceAssembler к каждому элементу (String), а не ко всей коллекции.

Есть ли способ достичь моей цели, используя материал Spring?

...