Spring Controller 注解是一个特化@成分注解。 Spring Controller 注解通常与基于 RequestMapping 注解的带注解的处理程序方法结合使用。
Spring Controller 注解只能应用于类。它用于将类标记为 Web 请求处理程序。它主要与春季MVC应用。
Spring @RestController是一个方便的注释,它本身注释为@控制器 and @ResponseBody。该注释用于将类标记为请求处理程序RESTful网页服务。
Let’s create a simple spring application where we will implement standard MVC controller as well as REST controller. Create a “Dynamic Web Project” in Eclipse and then convert it to Maven project. This will provide us with maven based web application structure and we can build our application on top of it. Below image shows the final project structure of our Spring MVC Controller application.
We would need following dependencies for our application.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Jackson for REST -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
让我们看看我们将在其中配置的部署描述符 (web.xml)DispatcherServlet
servlet作为前端控制器。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring-Controller</display-name>
<!-- Add Spring MVC DispatcherServlet as front controller -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
最后,我们有以下 spring 上下文文件。在这里,我们将应用程序配置为基于注释,并提供用于扫描 spring 组件的根包。我们也在配置InternalResourceViewResolver
bean 并提供视图页面的详细信息。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.journaldev.spring" />
<!-- Resolves views selected for rendering by @Controllers to JSP resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
我们的配置 XML 文件已准备就绪,现在让我们转到 Controller 类。
package com.journaldev.spring.controller;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/hello")
public String home(Locale locale, Model model) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
}
我们定义了一个请求处理程序方法,它接受带有 URI“/hello”的 GET 请求并返回“home.jsp”页面作为响应。请注意,我们正在为模型设置一个属性,该属性将在 home.jsp 页面中使用。这是我们简单的home.jsp
页面代码。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<p>The time on the server is ${serverTime}.</p>
</body>
</html>
Our conventional servlet based Spring MVC application with a simple controller is ready, just export it as the WAR file and deploy on Tomcat or any other servlet container. Then go to URL https://localhost:8080/Spring-Controller/hello
and you should see the following screen as output. ![Spring Controller Example](https://journaldev.nyc3.digitaloceanspaces.com/2018/06/Spring-Controller-Example.png)
现在让我们扩展我们的应用程序以公开 REST API。创建一个将作为 JSON 响应发送的模型类。
package com.journaldev.spring.model;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我们简单的 REST Controller 类。
package com.journaldev.spring.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.journaldev.spring.model.Person;
@RestController
public class PersonRESTController {
@RequestMapping("/rest")
public String healthCheck() {
return "OK";
}
@RequestMapping("/rest/person/get")
public Person getPerson(@RequestParam(name = "name", required = false, defaultValue = "Unknown") String name) {
Person person = new Person();
person.setName(name);
return person;
}
}
再次重新部署应用程序以测试我们的 REST API。
Go to URL https://localhost:8080/Spring-Controller/rest
and you should get following output.
Go to URL https://localhost:8080/Spring-Controller/rest/person/get
and you will get following JSON response:
Now let’s provide the name parameter value in the URL, go to https://localhost:8080/Spring-Controller/rest/person/get?name=Pankaj
and you will get following JSON response. ![Spring REST Controller Example](https://journaldev.nyc3.digitaloceanspaces.com/2018/06/Spring-RestController-Example.png)
Spring Controller 是 Spring MVC 应用程序的支柱。这是我们的业务逻辑开始的地方。此外,RestController 可以帮助我们轻松创建基于 Rest 的 Web 服务。
您可以从我们的下载示例项目代码GitHub 存储库.