When to use @RestController vs @RepositoryRestResource

后端 未结 4 1121
礼貌的吻别
礼貌的吻别 2020-12-07 10:34

I have been looking at various examples of how to use Spring with REST. Our end target is a Spring HATEOAS/HAL setup

I have seen two d

4条回答
  •  旧巷少年郎
    2020-12-07 10:55

    @RepositoryRestController override default generated Spring Data REST controllers from exposed repository.

    To take advantage of Spring Data REST’s settings, message converters, exception handling, and more, use the @RepositoryRestController annotation instead of a standard Spring MVC @Controller or @RestController

    E.g this controllers use spring.data.rest.basePath Spring Boot setting as base path for routing.

    See Overriding Spring Data REST Response Handlers.

    Be aware of adding @ResponseBody as it is missed in @RepositoryRestController

    If you not exposed repository (marked as @RepositoryRestResource(exported = false)), use @BasePathAwareController annotation instead

    Also be aware of bags

    ControllerLinkBuilder does not take Spring Data REST's base path into account and @RequestMapping shouldn't be used on class/type level

    and

    Base path doesn't show up in HAL

    Workaround to fix link: https://stackoverflow.com/a/51736503/548473

    UPDATE: at last I prefer not to use @RepositoryRestController due to lot of workarounds.

提交回复
热议问题