1.eclipse打开,进入一个工作空间,右键New --> Other
Maven --> Maven Project --> Next
Next
选择maven-archetype-quickstart --> Next
输入Group Id和Artifact Id --> Finish
刚创建完的项目是没有resources目录的,需要我们手动新建一个
项目右键 --> Build Path --> Configure Build Path
Source --> Add Folder
选中main --> Create New Folder --> 输入 resources
在src/main/resources目录下新建文件application.properties,该文件为项目配置文件
接下来,修改pom.xml文件 groupId和artifactId依照自己的项目填写,其他都是项目必须的最基本的一些包
<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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> </parent> <groupId>com.api</groupId> <artifactId>medium</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>com.api</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 针对pom文件头部报错解决方案,此错误不影响程序运行,仅对强迫症有害 --> <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
修改App.java,我修改了类名为MediumApplication,这个任意,只要有注解@SpringBootApplictaion就能成为一个启动类
package com.api.medium; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @SpringBootApplication public class MediumApplication { public static void main(String[] args) { SpringApplication.run(MediumApplication.class, args); } }
创建一个控制器,HelloController.java
package com.api.medium.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api") public class HelloController { @RequestMapping("hello") public String hello() { return "Hello World"; } }
右键MediumApplication.java 启动项目
打开postman测试接口端口为配置文件中的10018,返回 "Hello World" 测试成功
Tips:题外话,本文未涉及到跨域、传参、POST/GET请求限制、以及持久层框架Hibernate/MyBatis一类的问题,可以自行百度补充。