简体   繁体   中英

rest controller for Spring Data REST repository

I have implemented a simple Spring Data REST repository which works as expected and I am fine with it exposing all methods. This is what it looks like:

@RepositoryRestResource(path = "employees")
public interface EmployeeRepository extends PagingAndSortingRepository<Employees, Integer> 
{ }

Now I would like to wrap this repository in a controller, so I can later add Hystrix to it for fallbacks and exception handling. My issue is now, that I would like to keep the behavior of the repository above and just pass the response through the controller to the client. Is there a possible way without reimplementing all the methods of my repository (including sorting and pagination)?

This is what my controller currently looks like:

@RepositoryRestController
public class EmployeeController {

    private final EmployeeRepository repository;

    @Autowired
    public EmployeeController(EmployeeRepository repo) {
        repository = repo;
    }

    // Here I would like to return the same respone as my repository does
    @RequestMapping(method = GET, value = "/employees")
    public @ResponseBody ResponseEntity<?> parseRequest() {
        return ResponseEntity.ok("hi");
    }
}

It seems that you could simply call the method from your repository. Did you try it?

@RepositoryRestController
public class EmployeeController {

    private final EmployeeRepository repository;

    @Autowired
    public EmployeeController(EmployeeRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(method = GET, value = "/employees")
    public @ResponseBody ResponseEntity<List<Employee>> parseRequest() {
        List<Employee> employees = repository.getEmployees();
        return new ResponseEntity(employees, HttpStatus.OK);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM