REST stands for Representational State Transfer, which stands for “Representational State Transfer.” RESTful is a set of guidelines and constraints about the existing nature of the Web and how it can be used. Spring MVC RestController makes it easy to build RESTful applications.
Create projects using Maven
You can quickly create a project based on Spring Boot by using The Spring Initializer in IntelliJ IDEA (recommended).
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0 from</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1. RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Copy the code
The Spring Maven Plugin is a set of plugins developed by Spring for Maven. It contains several powerful features:
- You can quickly build an executable JAR package without too much complex configuration, allowing your application to run almost without being affected by the environment
- Automatic search
public static void main()
Method, and marks its class asStart the class - Spring Boot dependencies are managed automatically, with all dependency project versions consistent with the Spring Boot parent project (by default), although other versions can be manually specified
Spring also supports Gradle builds. For details, see Build with Gradle
Creating an entity Class
The entity class is not an ORM entity class, but a “resource” in REST. The function of our Web service is to process the GET request with the URL /userinfo/1 and return the result as a JSON response body. The response status code is 200 OK, and the JSON format is as follows:
{
"id": 1."name": "Zhang"
}
Copy the code
This example simply emulates getting User information with id 1 by creating the POJO class User:
public class User {
private final long id;
private final String name;
public User(long id, String name) {
this.id = id;
this.name = name;
}
public long getId(a) {
return id;
}
public String getName(a) {
returnname; }}Copy the code
By default, Spring uses' Jackson 'as a JSON parsing library to serialize POJO-class objects to JSON.Copy the code
Create a Controller
Serialize the return value as JSON and return it as the body of the response. The content-type of the return value is Application/JSON. Requesting /user/1 will get information about the user with ID 1. Here is the Controller class:
@RestController
public class GreetingController {
private static final String template = "Zhang";
private final long id = 1;
@RequestMapping("/user/{id}")
public User userInfo(@PathVariable("id")long id) {
return newUser(id, template); }}Copy the code
Let the app run
The traditional build method is to generate a war file and deploy it to the Web server, which is sometimes inconvenient. Therefore, it is recommended to use the Maven plug-in of Spring Boot to quickly generate a separate executable JAR file. Use the java-jar command to start the application. All classes and resource files are integrated into this jar file, which also includes the embedded servlet container (such as Tomcat). Here is the startup class for this application:
@SpringBootApplication
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
SpringBootApplication This annotation is a composite annotation that includes:
@Configuration
: declares this class to be the configuration class for the bean in context@EnableAutoConfiguration
: Have Spring configure or properties classes for the related beans scanned in the context and place them in the application context- Spring automatically adds spring-WebMVC dependencies to the application startup class when it detects them in the classpath
@EnableWebMvc
To indicate that the application is a Web application and that web-based operations, such as instantiation, are performed when the application is launchedDispatcherServlet
Class and perform related configurations @ComponentScan
: causes Spring to scan all customizationsComponent classes, configuration classes, business classes, and controllersAnd assemble it
In the main method of start classes call SpringBootApplication. The run () application startup can be realized, and the traditional Java web application configuration of complex web. Different XML file, do not need to spend too much time on configuration
Build an executable JAR
We can do this with a simple command:
$ ./mvnw clean package
Copy the code
Execute this command to start the application:
$Java jar target/gs - rest - service - 0.1.0 from. The jar
Copy the code
Do some simple tests
In the browser visit HTPP: / / localhost: 8080 / user / 1, if no problem will get the following response:
{
"id": 1."name": "Zhang"
}
Copy the code
conclusion
This is just a RESTful Web service. See Building a RESTful Web Service for more documentation
Personal blog updates synchronously, for more technical sharing please pay attention to: Bao Le Cheng’s blog