小编jco*_*oig的帖子

在Spring REST HATEOAS中使用RepositoryRestResource-s和常规控制器对root请求进行自定义响应

假设我有两个存储库:

@RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)

@RepositoryRestResource(collectionResourceRel = "person1", path = "person1")
public interface PersonRepository1 extends PagingAndSortingRepository<Person1, Long> {
    List<Person1> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)

使用一个常规控制器:

@Controller
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public HttpEntity<Hello> hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        Hello hello = new Hello(String.format("Hello, %s!", name));
        hello.add(linkTo(methodOn(HelloController.class).hello(name)).withSelfRel());
        return new ResponseEntity<>(hello, HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,回复http://localhost:8080/是:

{
  "_links" : …
Run Code Online (Sandbox Code Playgroud)

rest spring hateoas spring-hateoas spring-boot

28
推荐指数
1
解决办法
9160
查看次数

标签 统计

hateoas ×1

rest ×1

spring ×1

spring-boot ×1

spring-hateoas ×1