简体   繁体   English

REST 和 spring-mvc

[英]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 ?由于基于 REST 的控制器方法仅根据请求将对象(而不是视图)返回给客户端,我如何向我的用户显示视图? 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 ?或者也许更好的问题是什么是将 spring-mvc web 应用程序与 REST 结合的好方法,所以我的用户总是得到答案,不仅仅是(例如)JSON 格式,而且还有视图?

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.据我所知,基于 REST 的控制器非常适合移动应用程序(例如 twitter ),其中视图在应用程序内部处理,服务器唯一需要担心的是将正确的对象传递给正确的请求。 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.我可能在几件事上是错的(如果我错了,请纠正我),因为我正在尝试了解 REST 并且我仍在学习。

To simplify things - you basically have two options: 1) Build Spring MVC application.为了简化事情 - 您基本上有两个选择:1)构建 Spring MVC 应用程序。 2) Build REST backend application. 2) 构建 REST 后端应用程序。

In case of first option - within your application you will have both backend and frontend (MVC part).在第一个选项的情况下 - 在您的应用程序中,您将同时拥有后端和前端(MVC 部分)。

In case of second option you build only backend application and expose it through REST API.在第二个选项的情况下,您只构建后端应用程序并通过 REST API 公开它。 In most cases, you will need to build another application - REST client for your application.在大多数情况下,您需要为您的应用程序构建另一个应用程序 - REST 客户端。 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...这是一个更灵活的应用程序,因为它让您有机会从各种客户端访问您的后端应用程序 - 例如,您可以拥有 Android、IOS 应用程序,您可以使用 Angular 等实现 Web 应用程序......

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.请注意,thins 并不是那么简单,您可以在一个应用程序中同时拥有 REST 后端和 REST 客户端等......这只是非常非常简化,以便您获得一些总体情况。 Hope this clarified a little things.希望这澄清了一些事情。

There is some additional clarification related to REST and views worth learning.还有一些与 REST 和视图相关的额外说明值得学习。 From your question, I can see that you mean "view" in a sense of UI(user interface) and typical MVC usage.从您的问题中,我可以看出您指的是 UI(用户界面)和典型 MVC 用法意义上的“视图”。 But "view" can mean different things in a different contexts.但是“视图”在不同的上下文中可能意味着不同的东西。

So:所以:

  • JSON can be considered as a view for data JSON 可视为数据的视图
  • JSON is a representation of the resource, just like HTML is JSON 是资源的表示,就像 HTML 一样
  • JSON doesn't have style (unless you are not using a browser extension, which most the users are not using) JSON 没有样式(除非你没有使用浏览器扩展,大多数用户都没有使用)
  • The browser is recognizing HTML as a markup language and applying a style to it浏览器将 HTML 识别为标记语言并对其应用样式
  • Both are media types两者都是媒体类型
  • Both JSON and HTML are data formats JSON 和 HTML 都是数据格式
  • 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.如果您使用@ResponseBody注释方法,Spring 将使用 json 映射器来生成响应。 Instead of annotating every method with @ResponseBody you can annotate your class with @RestController .取而代之的注释与每个方法的@ResponseBody你可以注释你的类@RestController

If you want to return a view, you need to annotate the class with @Controller instead of @RestController and configure a viewresolver.如果要返回视图,则需要使用@Controller而不是@RestController来注释该类并配置一个viewresolver。 Bij default spring will use thymeleaf as a viewresolver if you have spring-web as a dependency on the classpath.如果您将 spring-web 作为类路径的依赖项,Bij 默认 spring 将使用 thymeleaf 作为视图解析器。 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 .模板存储在src/main/resources/templates

You can find a guide on the spring website: https://spring.io/guides/gs/serving-web-content/您可以在 spring 网站上找到指南: https : //spring.io/guides/gs/serving-web-content/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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