How to set default value of exported as false in rest resource spring data rest

前端 未结 4 715

I want to use RestResource annotation of spring data rest. As you know it exposes ALL CRUD methods by default. But I only need findAll method. One way is to set exported val

4条回答
  •  青春惊慌失措
    2021-01-20 09:02

    You should implement a custom controller for GET /questions request that will return only the result of findAll method, for example:

    @RequiredArgsConstructor
    @BasePathAwareController
    @RequestMapping("/questions")
    public class QuestionController {
    
        private final @NonNull QuestionRepository repository;
        private final @NonNull PagedResourcesAssembler assembler;
        private final @NonNull EntityLinks links;
    
        @GetMapping
        ResponseEntity get(Pageable pageable) {
            return ResponseEntity.ok(assembler.toResource(repository.findAll(pageable),
                    (ResourceAssembler) question -> 
                        new Resource<>(question,
                        links.linkToSingleResource(question).withSelfRel())));
        }
    }
    

    and disable your QuestionRepository to be exported:

    @RepositoryRestResource(exported = false)
    public interface QuestionRepository extends JpaRepository {
    }
    

    Working example.

提交回复
热议问题