0

Sorry in advance if this is a little confusing, it's difficult how to phrase this.

I am currently using Spring MVC with some RESTful services mixed in for some AJAX client side logic. I am looking to move towards a more SOA style of coding our views. Since the MVC and the REST service would live in the same application, is there a need to use Spring RestTemplate in the code when I need to populate objects into a Model?

Right now to GET User and display in the view, the Spring MVC talks to a DAO and then adds the User into Model. After reading a few things here on SO and Spring's Site it seems like RestTemplate could be used to get the data from a REST service and then insert into the model (if it came from a 3rd party).

Is there any reason why code should (or shouldn't) move to using the RestTemplate for internal REST calls instead of just using the DOA and such? I assume going towards SOA means moving into this style, but it seems a little clunky for calling a service that is within the same application. But I also see the value add of having the service exist once and using it multiple times, updating in one spot instead of all over so any front end web code would be automatically updated with any changes to the service layer.

Example:

@RequestMapping(value={"showUserTable"},method={RequestMethod.GET})
public String showUserTable(Model model){
  List<User> users = User.getAll(); //DAO nonsense here, assume List exists
  model.addAttribute("users", users);
  return "userTable"; // returns a jsp simply looping through the list and displaying.
}

Is there any reason to go towards complicating these to use the internal REST Service in favor of the encapsulation I gain of SOA, or is there a cleaner way to consume these services?

@RequestMapping(value={"showUserTable"},method={RequestMethod.GET})
public String showUserTable(Model model){
  List<User> users = restTemplate.getForObject("http://example.com/users", User.class);  // or however I use restTemplates, havent done it yet so still fuzzy but shouldnt be too tricky.    

  model.addAttribute("users", users);
  return "userTable"; // returns a jsp simply looping through the list and displaying.
}

migrated from stackoverflow.com Mar 11 '16 at 15:33

This question came from our site for professional and enthusiast programmers.

2

You are using Spring MVC and the rest service is in the same application, there's no need to do a rest call to get this data.

But if you wanna separate the service layer (creating a Rest API) and the view layer (Spring MVC) in 2 applications (modules), in this case it's needed. It's a good practice, you can expose your api for different clients, and native mobile applications, read more about that here:

http://microservices.io/patterns/microservices.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.