How to write paginated controller that expose resource or list of resource in spring-data-hatoas

非 Y 不嫁゛ 提交于 2019-12-22 13:32:19

问题


Using spring-data, I want to write two method for my Person entity.

Person.java:

public class Person {
    @Id
    String id;
    String name;
    Integer age;
    // getters/setters omitted for clarity
}

I have written also a PersonResouce:

public class PersonResource extends Resource<Person> {

    public PersonResource(Person content, Link... links) {
        super(content, links);
    }

}

I have also added a PersonResourceAssembler:

public class PersonResourceAssembler extends ResourceAssemblerSupport<Person, PersonResource> {

    public PersonResourceAssembler() {
        super(PersonController.class, PersonResource.class);
    }

    public PersonResource createResource(Person person) {
        PersonResource personResource = new PersonResource(person);
        Link link = linkTo(PersonController.class).slash(person.getId()).withSelfRel();
        personResource.add(link);
        return personResource;
    }

    @Override
    public PersonResource toResource(Person person) {
        PersonResource resource = createResource(person);
        return resource;
    }
}

And this is my PersonController:

@RestController
@RequestMapping("persons")
public class PersonController {

  @Autowired
  private PersonService personService;

  @GetMapping
  public HttpEntity<List<PersonResource>> showAll(@PageableDefault(size = 20) Pageable pageable, PersonDTO condition) {
    Page<Person> page = personService.findAll(pageable, condition);
    Iterable<Person> personList = page.getContent();
    PersonResourceAssembler assembler = new PersonResourceAssembler();
    List<PersonResource> resources = assembler.toResources(personList);
    return new HttpEntity<>(resources);
  }

  @RequestMapping(name = "{id}", produces= MediaType.APPLICATION_JSON_VALUE)
  public HttpEntity<PersonResource> showOne(@PathVariable("id") Long id, PersonDTO condition) {
    condition.setId(id);
    Person person = personService.get(id);
    PersonResourceAssembler assembler = new PersonResourceAssembler();
    PersonResource resource  = assembler.toResource(person);
    return new HttpEntity<>(resource);
  }

}

This is the response for the list:

[
  {
    "createdById": 1,
    "createdDate": "2017-09-21T10:21:05.741Z",
    "deleted": false,
    "email": null,
    "firstName": "User49",
    "id": 52,
    "lastModifiedById": null,
    "lastName": "robot",
    "links": [
      {
        "href": "http://localhost:8080/users/52",
        "rel": "self"
      }
    ],
    "middleName": null,
    "mobile": "010101010001149",
    "roleList": [
      {
        "createdById": null,
        "createdDate": "2017-09-21T10:21:05.580Z",
        "deleted": false,
        "id": 2,
        "lastModifiedById": null,
        "name": "USER",
        "userList": null,
        "version": 0
      }
    ],
    "username": "user49",
    "version": 0
  }
]

This is the response for one resource :

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/users/52"
    }
  },
  "createdById": 1,
  "createdDate": "2017-09-21T10:21:05.741Z",
  "deleted": false,
  "email": null,
  "firstName": "User49",
  "id": 52,
  "lastModifiedById": null,
  "lastName": "robot",
  "middleName": null,
  "mobile": "010101010001149",
  "roleList": [
    {
      "createdById": null,
      "createdDate": "2017-09-21T10:21:05.580Z",
      "deleted": false,
      "lastModifiedById": null,
      "name": "USER",
      "userList": null
    }
  ],
  "username": "user49"
}

I've looked at the documentation and it appear possible to use PagedResources to create pagination.

I also want my response to look like the RepositoryRestController response which means for the list reponse :

  • put entities under key "_embedded"
  • put links under key "_links"
  • put page under key "page"

I have tried to play a bit with PagedResources but it appear to work differently than Resource and can't just replace it.

I would like to see a Controller/Assembler that use the PagedResource.

Solution

solution for me was to do like this

  @GetMapping
  public ResponseEntity<?> findAll(PagedResourcesAssembler<Person> pageAssembler, @PageableDefault(size = 20) Pageable pageable, UserDTO condition) {
    Page<User> userList = userService.findAll(pageable, condition);
    PagedResources<?> resources = pageAssembler.toResource(userList, new UserResourceAssembler());
    return ResponseEntity.ok(resources);
  }

回答1:


Try to use PagedResourcesAssembler to build paged resources:

@RestController
@RequestMapping("persons")
public class PersonController {

    @Autowired private PersonService personService;
    @Autowired private PagedResourcesAssembler<Person> assembler;
    @Autowired private EntityLinks links;

    @GetMapping("/paged")
    public ResponseEntity<?> getPaged(Pageable pageable) {
        Page<Person> personsPage = personService.getPaged(pageable);

        Link pageSelfLink = links.linkFor(Person.class).slash("/paged").withSelfRel();
        PagedResources<?> resources = assembler.toResource(personPage, this::toResource, pageSelfLink);

        return ResponseEntity.ok(resources);
    }

    private ResourceSupport toResource(Person person) {
        Link pesonLink = links.linkForSingleResource(person).withRel("person");
        Link selfLink = links.linkForSingleResource(person).withSelfRel();
        return new Resource<>(person, personLink, selfLink);
    }
}

See my example.



来源:https://stackoverflow.com/questions/46342114/how-to-write-paginated-controller-that-expose-resource-or-list-of-resource-in-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!