将PagedResourcesAssembler与List <string>内容一起使用

时间:2018-11-28 14:22:08

标签: java spring spring-hateoas

我正在使用Spring Boot 2,Springa Data REST,Spring HATEOAS。 我有一个特定的@RepositoryRestController,我需要在其中返回String的分页结果。所以我没有反对,而是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应用于每个项目(字符串),而不是整个集合。

使用Spring的东西有没有办法达到我的目标?

0 个答案:

没有答案