目标:用spring-boot搭建一个支持get接口请求的服务。
1.构造一个响应体对象:
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.接口请求响应封装controller:
package com.example.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@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));
}
}
3.服务应用启动程序:
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);
}
}
4.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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rest-service-complete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-service-complete</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>
5.服务部署并启动:
mvnw spring-boot:run
6.通过get请求访问服务:
http://localhost:8080/greeting?name=Users
Up:
@RestController:
相当于@Controller+@ResponseBody两个注解的结合,但不能返回jsp,html页面,视图解析器无法解析jsp,html页面
@GetMapping:
用于处理请求方法的GET类型
@RequestParam:
获取springmvc后台数据
@SpringBootApplication:
开启自动配置
Outer:
@Controller和@RestController的区别?
参考:@Controller和@RestController的区别?_hery186的专栏-优快云博客_restcontroller
@GetMapping注解的理解
参考:@GetMapping注解的理解_水巷石子的博客-优快云博客_getmapping
@RequestParam注解的使用:
参考:@RequestParam 注解的使用_Lovincc的博客-优快云博客
@SpringBootApplication启动类执行流程:
参考:SpringBoot 启动类 @SpringBootApplication 注解 以及执行流程_殇莫忆的博客-优快云博客_springbootapplication