0

I have developed 2 microservices in Spring:

  • a UI-service
  • a Login-service, which also has UI (html-form) for testing

The UI-service is consuming the Login-service.

UI-service (code)

@RequestMapping("/log")
public String abc(HttpServletRequest request) {
    final String uri = "http://localhost:8093/accounts/login";

    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);

    return result;
    //request.setAttribute("mode", "MODE_LOGIN");
}


@RequestMapping("/login-user")
private String getEmployees1(HttpServletRequest request) {
    final String uri = "http://localhost:8093/login-user";
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);

    return result;
}

Login-service (code)

I have this working on port 8093.

@RequestMapping("/accounts/login") //login 
public String login(HttpServletRequest request) {
    request.setAttribute("mode", "MODE_LOGIN");
    return "login";
}

@RequestMapping ("/login-user") // checking user details in database
public String loginUser(@ModelAttribute User user, HttpServletRequest request) {
    //ModelAndView model = new ModelAndView();
    if(userService.findByEmailAndPassword(user.getEmail(), user.getPassword())!=null) {
        return "index";
    }
    else {
        request.setAttribute("error", "Invalid username and password!");
        request.setAttribute("mode", "MODE_LOGIN");
        return "login";
    }
}

I have placed following attributes MODE_HOME and MODE_LOGIN in the JSP-page.

Same code if i test with Login Service with its UI, it is working fine but while consuming from UI service it is taking null value. I am able to consume the service but following value is showing in the console (console.log) when same service is used:

select user0_.id as id1_0_, user0_.`email` as email2_0_, user0_.`fname` as fname3_0_, 
user0_.`lname` as lname4_0_, user0_.`pwd` as pwd5_0_, user0_.`phone` as phone6_0_ from user 
user0_ where user0_.`email`=? and user0_.`pwd`=?

But when consuming the Login-service from another using restTemplate:

select user0_.id as id1_0_, user0_.`email` as email2_0_, user0_.`fname` as fname3_0_, 
user0_.`lname` as lname4_0_, user0_.`pwd` as pwd5_0_, user0_.`phone` as phone6_0_ from user user0_ 
where (user0_.`email` is null) and (user0_.`pwd` is null)

I am new to this.

What could be the issue?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Manish Goyal
  • 69
  • 1
  • 9
  • Your login service appears to be expecting a `User` object to exist in the request, but it doesn't look like you're sending any user information when you call the login service from your UI service. – Jordan Jan 08 '20 at 20:25
  • @Jordan how can i send that? – Manish Goyal Jan 08 '20 at 20:26

1 Answers1

1

Issue

Your code calls the Login-service without query-parameters, just with a base-URL:

http://localhost:8093/login-user

As a result the model User will be created, but its fields email and password are not set (both are still null).

If then passing the User object to your SQL-query, the respective parameters are also evaluated to null.

Thus on the console log the resulting SQL is shown also with null where instead some valid values would be expected.

Solution

Pass needed query-parameters email and password to the call of restTemplate.getForObject.

Either by using Spring's RestTemplate's method getForObject(String url, Class<T> responseType, Map<String,?> uriVariables) by passing the query-parameters to getForObject as map:

Map<String, Object> parameterMap = new HashMap<>();
parameterMap.put("email", "theEmail@domain.com");
parameterMap.put("password", "secret");

restTemplate.getForObject(uri, String.class, parameterMap);

Or use Spring's UriComponentsBuilder to build the complete URL (including query-parameters) and pass it like this:

UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(uri)
  .queryParam("email","theEmail@domain.com")
  .queryParam("password","secret")
  .build();

restTemplate.getForObject(uriComponents.toUriString(), String.class);

In both approaches the resulting HTTP GET will use following URL:

http://localhost:8093/login-user?email=theEmail@domain.com&password=secret

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38