学习springboot
创建一个spingboot项目的链接
1 在templates文件夹中写index.html
![在这里插入图片描述](https://img-blog.csdnimg.cn/5fd96ed7a4f444068b6c6d1bcdfd5516.png
1.1 编写index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello</h1>
</body>
</html>
1.2 编写application.yml
spring:
web:
resources:
static-locations: classpath:/templates
1.3 编写HelloController类
package com.usc.demo01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloControl {
@RequestMapping
public String index(){
return "index.html";
}
}
1.4运行
运行后在浏览器输入 localhost:8080/
2 在test中写测试文件(有自动装配)
2.1 编写Person和Dog类
package com.usc.demo01.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix="person")
@Validated
public class Person {
//@Email(message="name格式不正确")
@NotNull(message = "不能为空")
private String name;
private int age;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public Person(String name, int age, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Map<String, Object> getMaps() {
return maps;
}
public List<Object> getLists() {
return lists;
}
public Dog getDog() {
return dog;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
package com.usc.demo01.pojo;
import org.springframework.stereotype.Component;
@Component
public class Dog {
private String name;
private int age;
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.2 编写pom.xml
修改版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
解决springboot配置注解处理器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
解决版本修改依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<type>pom</type>
</dependency>
解决验证依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
2.3 修改application.yml
person:
name: wxy${random.uuid}
age: ${random.int}
maps: {k1: v1,k2: v2}
lists:
-java
-c
-python
dog:
name: qyq
age: 87
2.4编写DemoApplicationTests类
package com.usc.demo;
import com.usc.demo.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
2.5运行
右键DemoApplicationTests类
这一行显示输出