本节内容
- 修改程序访问端口
-http://localhost:8080/hello
修改为http://localhost:8085/hello
- 修改程序访问的路径
-http://localhost:8085/hello
修改为http://localhost:8085/tigerkin/hello
- 自定义属性绑定
-tiger.map.name=张三
绑定到变量上
整体步骤
- 新建一个基础SpringBoot项目
- 引入web依赖
spring-boot-starter-web
- 修改
application.properties
文件 - 新建接口访问类
HelloController.java
- 运行项目,浏览器访问测试
具体步骤
修改程序访问端口
- 修改pom.xml,引入web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 修改application.properties,添加
server.port=8085
#将默认的8080端口访问修改成8085
server.port=8085
- 新建HelloController.java,
/hello
用来访问测试
package com.yejx.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello world Spring Boot!";
}
}
-
运行启动类
Tigerkin02Application.java
-
打开浏览器访问http://localhost:8085/hello
修改程序访问的路径
- 基于修改端口的操作,修改application.properties,添加
server.servlet.context-path=/yejx
server.port=8085
server.servlet.context-path=/tigerkin
- 运行启动类
Tigerkin02Application.java
- 打开浏览器访问http://localhost:8085/tigerkin/hello
自定义属性绑定
- 修改application.properties文件,添加如下代码
# 随机数
com.yejx.age.number=${random.int}
#Map
##方式一
tiger.map.name=张三
tiger.map.age=1
##方式二
tiger.map[name]=张三1
tiger.map[age]=11
#List
##方式一
tiger.list[0]=一
tiger.list[1]=二
tiger.list[2]=三
##方式二
tiger.list=一1,二1,三1
- 新建Tiger.java,
package com.yejx.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//注册该类到spring容器中,方便测试时使用
@Component
//该注解配置属性前缀,会根据前缀自定匹配对应的变量
@ConfigurationProperties(prefix = "tiger")
public class Tiger {
private Map<String,Object> map = new HashMap<>();
private List<String> list = new ArrayList<>();
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
3.新建测试类Tigerkin02ApplicationTests.java
package com.yejx.demo;
import com.alibaba.fastjson.JSONObject;
import com.yejx.pojo.Tiger;
import com.yejx.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
@SpringBootTest
class Tigerkin02ApplicationTests {
@Autowired
private User user;
@Autowired
private Tiger tiger;
@Test
void contextLoads() {
System.out.println(user.toString());
System.out.println(JSONObject.toJSONString(tiger.getMap()));
System.out.println(Arrays.toString(tiger.getList().toArray()));
}
}
- 运行Tigerkin02ApplicationTests.java测试类
控制台输出如下
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-05-24 22:16:56.426 INFO 11444 --- [ main] c.yejx.demo.Tigerkin02ApplicationTests : Starting Tigerkin02ApplicationTests using Java 1.8.0_202 on yejx with PID 11444 (started by 11432 in D:\work\file\document\yejx\开发学习\springboot\SpringBoot\tigerkin\tigerkin02)
2021-05-24 22:16:56.429 INFO 11444 --- [ main] c.yejx.demo.Tigerkin02ApplicationTests : No active profile set, falling back to default profiles: default
2021-05-24 22:16:57.565 INFO 11444 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-24 22:16:57.883 INFO 11444 --- [ main] c.yejx.demo.Tigerkin02ApplicationTests : Started Tigerkin02ApplicationTests in 1.733 seconds (JVM running for 2.799)
User{name='yejingxuan', age=20}
{"name":"张三","age":"1"}
[一1, 二1, 三1]
2021-05-24 22:16:58.118 INFO 11444 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Disconnected from the target VM, address: '127.0.0.1:52570', transport: 'socket'
本文对应demo在仓库中tigerkin02目录中:gitee仓库地址