文章目录
前言
本身不是后端人,但是在公司项目上用的是springboot的框架,而且感觉还可以,所以经常会在网上百度一些springboot的问题,发现大多是解决不了根本;所以需要系统性的学习,而系统性的学习,又很多是视频,得不到要点,所以这里我就讲官方的example走一边,算是翻译,也算是为以后的初学者们做一点贡献;主要参考流程是官网的demo以及入门指南
https://spring.io/guides#getting-started-guides
构建restful web服务
其实很简单,我希望通过springboot搭建一个服务,能够调用他的接口,返回给我的参数即可;
需要:
1 jdk 1.8
2 IDEA
3 maven
这些东西自己搭建
步骤
1 利用spring官网的初始化插件构建一个初始的springboot框架
参考地址:https://start.spring.io/
maven创建的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.3.2.RELEASE</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 创建对应的请求
这里是我创建好的,具体的demo,见我的博文:
https://blog.csdn.net/wangbiao9292/article/details/110947040
这里的重点,主要是说明一些注解的作用跟着官网指南:
(1) @RequestMapping确保将HTTP GET请求/test映射到该test()方法。说白了,在你前端发出请求时:http://127.0.0.1/tset,这时,为什么能够访问到test()方法,就是@RequestMapping的作用
注意:常见的还有:
1 @GetMapping
2 @PostMapping
3 @RequestMapping(method=GET) 默认为GET
为了方便大家看,这里我截取一张图供大家参考: