建立第一个springboot项目
把已经内容复制到pom.xml中,
<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>
<groupId>org.dai</groupId>
<artifactId>cmd.collect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加项目的启动类
package org.dai.cmd.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 下面的scanBasePackages 指定spring自动扫描的包,spring默认扫描启动类所在的包及子包
* @author daipl
*
*/
@SpringBootApplication(scanBasePackages = "org.dai.cmd")
public class CollectApplication {
public static void main(String[] args) {
SpringApplication.run(CollectApplication.class, args);
}
}
添加一个service
package org.dai.cmd.auth.service;
import org.dai.cmd.auth.vo.UserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* restController相当于@Controller and @ResponseBody
* 程序返回数据本身,而不是返回一个view,(即返回的是json数据)
*
* @author daipl
*
*/
@RestController
@RequestMapping("/userService")
public class UserService {
@RequestMapping("/findUserInfo")
public UserVO findUserInfo() {
return new UserVO();
}
}
/**
uservo如下
*/
class UserVO {
private Long useId;
private String userName;
private String passWord;
private Long devId;
public Long getUseId() {
return useId;
}
public void setUseId(Long useId) {
this.useId = useId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public Long getDevId() {
return devId;
}
public void setDevId(Long devId) {
this.devId = devId;
}
}
添加application.properties文件,配置项目的基本属性
#tomcat端口号
server.port=8080
#项目根目录
server.servlet.context-path=/col
http://localhost:8080/col/userService/findUserInfo
访问以上连接,返回json数据
以jar包发布springboot项目时,默认会先使用jar包跟目录下的application.properties来作为项目配置文件。
如果在不同的目录中存在多个配置文件,它的读取顺序是:
1、config/application.properties(项目根目录中config目录下)
2、config/application.yml
3、application.properties(项目根目录下)
4、application.yml
5、resources/config/application.properties(项目resources目录中config目录下)
6、resources/config/application.yml
7、resources/application.properties(项目的resources目录下)
8、resources/application.yml