简体   繁体   中英

REST and spring-mvc

Since REST based controller methods only return objects ( not views ) to the client based on the request, how can I show view to my user ? Or maybe better question what is a good way to combine spring-mvc web app with REST, so my user always get the answer, not in just ( for example ) JSON format, but also with the view ?

So far as I understood, REST based controller would be perfectly fitting to the mobile app ( for example twitter ), where views are handled inside the app and the only thing server has to worry about is to pass the right object to the right request. But what about the web app ?

I might be wrong in several things ( correct me if I am ), since I am trying to understand REST and I am still learning.

To simplify things - you basically have two options: 1) Build Spring MVC application. 2) Build REST backend application.

In case of first option - within your application you will have both backend and frontend (MVC part).

In case of second option you build only backend application and expose it through REST API. In most cases, you will need to build another application - REST client for your application. This is more flexible application because it gives you opportunity to access your backend application from various clients - for example, you can have Android, IOS applications, you can have web application implemented using Angular etc...

Please note, that thins are not so simple, you can within one application have both REST backend and REST client etc... This is just very very simplified in order that you get some general picture. Hope this clarified a little things.

There is some additional clarification related to REST and views worth learning. From your question, I can see that you mean "view" in a sense of UI(user interface) and typical MVC usage. But "view" can mean different things in a different contexts.

So:

  • JSON can be considered as a view for data
  • JSON is a representation of the resource, just like HTML is
  • JSON doesn't have style (unless you are not using a browser extension, which most the users are not using)
  • The browser is recognizing HTML as a markup language and applying a style to it
  • Both are media types
  • Both JSON and HTML are data formats
  • Both can be transferred over the wire

This method returns a view

@RequestMapping("/home")
String home(Model model) {  
    return "home";  // resources\templates\home.html
}

This method Returns String

@RequestMapping(value = "/home")
@ResponseBody
public String home() {
    return "Success";
}

If you annotate a method with @ResponseBody , Spring will use a json mapper to generate the response. Instead of annotating every method with @ResponseBody you can annotate your class with @RestController .

If you want to return a view, you need to annotate the class with @Controller instead of @RestController and configure a viewresolver. Bij default spring will use thymeleaf as a viewresolver if you have spring-web as a dependency on the classpath. The return type of the method is a String that references the template to be rendered. The templates are stored in src/main/resources/templates .

You can find a guide on the spring website: https://spring.io/guides/gs/serving-web-content/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM