一、从start.spring.io下载相应的.zip文件。
二、解压.zip文件并将相应内容导入到eclipse或myeclipse中,pom.xml文件如下。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54<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>com.xzw</groupId> <artifactId>studysb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>studysb</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
三、编写代码实现简单的API
代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package com.xzw.springboot; import java.util.Date; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/xzw") @EnableAutoConfiguration public class StudySpringBoot02 { @RequestMapping("/name") public String index(){ return "I am xzw"; } @SuppressWarnings("deprecation") @RequestMapping("/date") String nowDate(){ return "现在的时间是:" + (new Date()).toLocaleString(); } @GetMapping("/person/{name}/{height}/{address}") public String getInfo(@PathVariable String name, @PathVariable Double height, @PathVariable String address){ return "姓名:" + name + ",身高:" + height + ",家庭住址:" + address; } @GetMapping("/person/{name}-{height}-{address}") public String getInfo02(@PathVariable String name, @PathVariable Double height, @PathVariable String address){ return "姓名:" + name + ",身高:" + height + ",家庭住址:" + address; } public static void main(String[] args) { SpringApplication.run(StudySpringBoot02.class, args); } }
运行结果如下:
至此,一个用SpringBoot开发的简单的服务API就完成了。
你们在此过程中还遇到了什么问题,欢迎留言,让我看看你们都遇到了哪些问题。
最后
以上就是唠叨香烟最近收集整理的关于使用Spring Boot快速开发模式开发简单的服务API的全部内容,更多相关使用Spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复