现在官网上的Quickstart Guide https://spring.io/quickstart
已经都推荐Eclipse,或者idea自动生成代码了
但是还是喜欢敲代码敲出来一个干净的项目
在pom.xml最上面添加
<parent>
<groupId>cn.dails</groupId>
<artifactId>dails-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath></relativePath>
</parent>
这是自己定义的一个parent,用于jar包的版本控制
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>dms-user</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在application.yml中添加
server:
port: 28081
spring:
application:
name: dms-user
新建启动类
package cn.dms;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run( UserApplication.class, args );
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "屌丝") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}
好了,正常启动吧
访问http://127.0.0.1:28081/hi
-
可以在yml里面设置server.base-uri-code=l***
在路径上配置
@RequestMapping(“/${server.base-uri-code}/private/bonus”)
这样达到统一路径 -
还有个习惯:public/private 塞到路径上
public不用鉴权
private 需要鉴权
server.servlet.context-path定义上下文路径
server.servlet.context-path不配置时,默认为 / ,如:localhost:8080/xxxxxx
当server.servlet.context-path有配置时,比如 /api,此时的访问方式为localhost:8080/api/xxxxxx
- 历史区别、springboot 2.0变革后的配置区别
1、springboot 2.0之前,配置为 server.context-path
2、springboot 2.0之后,配置为 server.servlet.context-path