如何编写在spring-data-hatoas

时间:2017-09-21 10:55:43

标签: spring spring-mvc spring-data spring-data-rest spring-hateoas

使用spring-data,我想为我的Person实体编写两个方法。

Person.java:

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

我还写了PersonResouce

public class PersonResource extends Resource<Person> {

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

}

我还添加了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;
    }
}

这是我的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);
  }

}

这是列表的回复:

[
  {
    "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
  }
]

这是一个资源的响应:

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

我查看了documentation,似乎可以使用 PagedResources 来创建分页。

我还希望我的回复看起来像RepositoryRestController响应,这意味着列表响应:

  • entities放在关键字“_embedded”
  • links放在关键字“_links”
  • page置于关键“页面”

image

我尝试使用PagedResources稍微玩一下,但它似乎与Resource的工作方式不同,并且不能只替换它。

我希望看到一个使用PagedResource的Controller / Assembler。

解决方案

我的解决方案是这样做

  @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 个答案:

答案 0 :(得分:2)

尝试使用PagedResourcesAssembler构建分页资源:

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

请参阅我的 example