spring-bootmicroservices

How to fix "blank page" in Spring Boot UI-microservices running on same tomcat instance


Background:

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.

Trying to accomplish:

Issue that is occurring:

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;
}

Expected Result:

I'm excepting to see the welcome page from "MODULE_1" once I click the button - "Go to Module1" from main page


Solution

  • 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:

    1. From the login controller return the response obtained by calling module 1 API using RestTemplate. Preferred if the target API is running in different server/port.
    2. From the login controller forward the request to the module 1 controller by returning a String "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