I've created multiple Spring-boot microservices for UI and trying to communicate with each other. Microservices are: Login_MODULE, MODULE_1, MODULE_2 etc. All of them are packaged as war and deployed on tomcat within eclipse.
Login_MODULE: It's for login and main (landing) page. User will enter his credential and upon successful login, he will see the main page; on the main page, there will be 4 different buttons. Clicking any of the button will take user to the appropriate module. Buttons are: "Go to Module1", "Go to Module2", "Go to Module3", "Go to Module4". My thought was to make each module as a microservice.
2nd microservice: for "MODULE 1"
Login_MODULE is working fine. I see the main-page where I can click the buttons to go to the different modules. Also for the 2nd microservice, if I explicitly hit http://localhost:8080/module1/empCenter/user1 (on different browser tab), I see the welcome page (which is expected).
But the issue is, if I try to access the module-1 by clicking on the "Go to Module1", I don't see that welcome page; I get the blank page. No exception thrown. Even though (while debugging) I see it goes to the controller in "MODULE_1" and call that view.
Below is the restController from Login microservice from where I call the "Module_1":
@RestController
public class WelcomeRestController {
Logger logger = LoggerFactory.getLogger(WelcomeRestController.class);
@Autowired
private RestTemplate restTemplate;
@GetMapping("/employeeCenter")
public void openEmployeeCenterApp() {
logger.info("Invoking EmployeeCenter micro-service");
ResponseEntity<Void> response = restTemplate.getForEntity("http://localhost:8080/module1/empCenter/user1", Void.class);
}
And here is the controller from MODULE_1 microservice:
@Controller
public class Module1Controller {
@RequestMapping("/empCenter/{username}")
public ModelAndView welcome(@PathVariable("username") String username) {
ModelAndView modelAndView = new ModelAndView("welcome");
return modelAndView;
}
I'm excepting to see the welcome page from "MODULE_1" once I click the button - "Go to Module1" from main page
I see the module 1 controller
is returning a view. When module 1 API is directly invoked from the browser you see the response. However, when login controller
is invoking module 1 API it gets the response and does nothing with the response. It has to return the obtained response. This is the reason you don't get to see anything in the browser. Neither there is any failure.
The options are:
RestTemplate
. Preferred if the target API is running in different server/port."forward:/module_1_API_URL"
or a ModelAndView("forward:/module_1_API_URL")
Here are various ways for forwarding request:
Call API, capture and return response:
@RequestMapping(path = "/greetings2")
@ResponseBody
public String greetings2(ModelMap modelMap) {
return restTemplate.getForEntity("http://localhost:92/template2", String.class).getBody();
}
Forward Option 1:
@RequestMapping(path = "/greetings")
public ModelAndView greetings() {
return new ModelAndView("forward:/template");
}
Forward option 2:
@RequestMapping(path = "/greetings1")
public String greetings1() {
return "forward:/template1";
}
The running samples are available at: github
git clone https://github.com/fiveobjects/reference.git
cd java/springboot
mvn spring-boot:run
URLs for Greetings:
http://localhost:92/greetings
http://localhost:92/greetings1
http://localhost:92/greetings2
Greetings APIs get the actual response from following template APIs.
http://localhost:92/template
http://localhost:92/template1
http://localhost:92/template2