附源码:构建一个简单的spring-boot的RESTful Web Service项目
目标:
构建一个简单的spring-boot的RESTful Web Service项目
了解spring-boot的restful web service和web项目的区别
1. 初始化源代码
从spring网站初始化和下载源代码
主要依赖:spring web
将下载好的源代码作为maven项目导入idea,经修改后的结构如下:
pom.xml依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rest-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:application.properties为空;
源代码:RestserviceApplication.java文件是项目启动入口
关键源代码及解析如下:
RestserviceApplication.java
-
@SpringBootApplication=@Configuration+@EnableAutoConfiguration+@ComponentScan
-
@Configuration 标识类作为application context的bean定义源
-
@EnableAutoConfiguration 告诉spring boot添加的beans要基于classpath settings、其他beans、不同的property设置。比如,如果spring-webmvc在classpath中,这标志着应用程序是一个web应用,还会激活关键行为,如设置DispatcherServlet。
-
@ComponentScan 告诉spring在该注解标识的类所在的包内查找其他组件、配置、service,并找到控制器
-
-
从main启动spring的方法中可以看到:没有web.xml,该项目是一个100%纯java的项目,无需做任何通道和基础配置
package com.example.restservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestserviceApplication {
public static void main(String[] args) {
SpringApplication.run(RestserviceApplication.class, args);
}
}
GreetingController.jar
- 传统的MVC形式的Controller和RESTful web service形式的Controller的不同之处:
- 前者会创建一个HTTP响应体,他依赖于视图技术把server端响应数据渲染成html,而后者则是填充和返回一个对象,该对象的数据会以JSON形式直接写入HTTP响应;响应对象转换为JSON由Spring支持,无需手动转换
- @RestController注解会把类标识为一个controller,该controller中每一个方法都会返回一个域对象,而不是一个view视图
- @RestController=@Controller+@ResponseBody
- @GetMapping("/greeting")
- 标识http的get请求“greeting”会被映射到方法greeting()来处理
- @GetMapping=@RequestMapping(method=RequestMethod.GET)
- 对于其他请求行为还有其他配套的注解,如@PostMapping
- @RequestParam
- 绑定请求参数name到greeting()方法的name参数上,若请求参数中没有那么参数,则会使用默认值绑定到greeting()方法的name参数上
package com.example.restservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name){
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
Greeting.java
- RESTful web service响应的一个VO对象
package com.example.restservice;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content){
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent(){
return content;
}
}
2. 源代码启动RestserviceApplication
浏览器或其他项目http请求调用访问:http://localhost:8080/greeting;
浏览器或其他项目http请求调用传参:http://localhost:8080/greeting?name=xiaoming
3. maven启动项目
命令行模式下切入项目源代码路径下;
执行命令:
mvnw spring-boot:run
启动后如步骤2访问项目即可
4. maven构建一个可执行jar包并启动
命令行模式下切入项目源代码路径下;
执行命令:
## 构建一个可执行jar包
mvnw clean package
## 启动可执行jar包
java -jar target/rest-service-0.0.1-SNAPSHOT.jar
启动后如步骤2访问项目即可