Search code examples
springrestspring-bootmicroservices

Call REST API from another project


I have two projects one for user and another for department.

Now I want to call a REST API from another REST API. How can I call?

I am facing problem because user related classes are not available for department and vice versa.

This is my UserController class.

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired   
    UserService userService;
    @GetMapping(value="/get", headers="Accept=application/json")
    public List<User> getAllUser() {
        List<User> tasks=userService.getUser();
        return tasks;
    }

And this is my DepartmentController class.

@RestController
@RequestMapping("/dept")
@Configurable
public class DeptController {
@Autowired
    DeptServiceImpl deptService;
@GetMapping(value="/get", headers="Accept=application/json")
        public List<User> getDept() {
            List<Department> tasks=deptService.getDept();
            return tasks;
        }

Please tell me how to call getUser() method in getDept(), and how to make classes available for each other.


Solution

  • you need to expose your microservice resources as rest APIs so you can call them from outside, and then you can use RestTemplate from spring to retrieve your information

    EDIT

    here is a good example you can rely on to achieve what you want