自定义Spring HATEOAS响应

时间:2017-05-03 13:36:18

标签: java spring spring-mvc hateoas

我有一个像这样的控制器:

@RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Resources<StudentResource>> getList(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "sort", required = false) String sort,
            @RequestParam(value = "sort.dir", required = false) String sortOrder,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "username", required = false) String username

            //@RequestParam(required=false) Map<String, String> searchParams
            ) {

        Map<String, String> searchParams = new HashMap<>();
        if(username != null) searchParams.put("username", username);
        if(name != null) searchParams.put("name", name);

        PageSearchModel model = new PageSearchModel(page, sort, sortOrder, searchParams);

        List<Student> students = studentService.getList(model);

        if (students == null)
            return null;

        List<StudentResource> resources = students.stream()
                .map(student -> studentResourceAssembler.toResource(student))
                .collect(Collectors.toList());

        Link selfLink = linkTo(methodOn(StudentController.class)
                .getList(null,null,null,null,null))
                .withSelfRel();

        return new ResponseEntity(new Resources<StudentResource>(resources, selfLink), HttpStatus.OK);
    }

这些参数用于分页和搜索,我想要的是生成一个包含所有参数的自我链接,即使它们是选项,但我得到的是:

"_links": {
    "self": {
      "href": "http://localhost:8080/students{?page,sort,sort.dir,name,username}"
    }   

}

还有一种方法可以在链接中包含HttpMethod吗?

编辑:同样对于POST方法,例如,要插入新的学生,有没有办法在链接中包含模型的结构?

0 个答案:

没有答案