这里是结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/086f8e63ea035f61d266a313f8935d8e.png)
DemoApplication.class
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Person.class
package com.example.demo.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String,Object> score;
private Set<Double> salaries;
private Map<String,List<Pet>> allPets;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public List<String> getAnimal() {
return animal;
}
public void setAnimal(List<String> animal) {
this.animal = animal;
}
public Map<String, Object> getScore() {
return score;
}
public void setScore(Map<String, Object> score) {
this.score = score;
}
public Set<Double> getSalaries() {
return salaries;
}
public void setSalaries(Set<Double> salaries) {
this.salaries = salaries;
}
public Map<String, List<Pet>> getAllPets() {
return allPets;
}
public void setAllPets(Map<String, List<Pet>> allPets) {
this.allPets = allPets;
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
", boss=" + boss +
", birth=" + birth +
", age=" + age +
", pet=" + pet +
", interests=" + Arrays.toString(interests) +
", animal=" + animal +
", score=" + score +
", salaries=" + salaries +
", allPets=" + allPets +
'}';
}
}
Pet.class
package com.example.demo.bean;
public class Pet {
private String name;
private Double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
", weight=" + weight +
'}';
}
}
HelloController.class
package com.example.demo.controller;
import com.example.demo.bean.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
final Person person;
public HelloController(Person person) {
this.person = person;
}
@RequestMapping("/person")
public Person person() {
return person;
}
@RequestMapping("/1.png")
public String hello() {
return "123";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getUser() {
return "get-张三";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String saveUser() {
return "post-张三";
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public String putUser() {
return "put-张三";
}
@RequestMapping(value = "/user", method = RequestMethod.DELETE)
public String deleteUser() {
return "delete-张三";
}
}
ParameterTestController.class
package com.example.demo.controller;
import org.springframework.boot.web.server.Cookie;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class ParameterTestController {
@RequestMapping("/car/{id}/owner/{username}")
public Map<String, Object> getCar(
@PathVariable("id") Integer id,
@PathVariable("username") String name,
Map<String, String> pv,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map<String, String> head,
@RequestParam("age") Integer age,
@RequestParam("inters") List<String> inters,
@RequestParam Map<String, String> params,
@CookieValue("_ga") String _ga) {
Map<String, Object> map = new HashMap<>();
map.put("age", age);
map.put("inters", inters);
map.put("params", params);
map.put("ga", _ga);
System.out.println(map);
return map;
}
@PostMapping("/save")
public Map postMethod(@RequestBody String context){
Map<String,Object> map = new HashMap<>();
map.put("context",context);
return map;
}
}
RequestController.clsss
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
public class RequestController {
@GetMapping("/goto")
public String goToPage(HttpServletRequest request){
request.setAttribute("msg","成功了···");
request.setAttribute("code",200);
return "forward:success";
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code,
HttpServletRequest request){
Object msg1 = request.getAttribute("msg");
Map<String,Object> map = new HashMap<>();
map.put("reqMethod_msg",msg1);
map.put("annotation_msg",msg);
return map;
}
}
application.yml
person:
userName: 张三
boss: true
birth: 2019/12/9
age: 25
interests:
- 篮球
- 足球
- 18
animal: [ 阿猫,阿狗 ]
score: { english: 80,math: 90, }
salaries:
- 9999.98
- 9999.99
pet:
name: 阿狗,
weight: 99.99
allPets:
sick:
- { name: 阿狗,weight: 99.99 }
- name: 阿猫
weight: 88.8
- name: 啊虫
weight: 77.77
health:
- { name: 阿花,weight: 199.9 }
- { name: 阿明,weight: 199.9 }
spring:
banner:
image:
location:
bitdepth: 4
cache:
type: redis
redis:
time-to-live: 11000
resources:
static-locations:
[ classpath: /custom/ ]
add-mappings: true
cache:
period: 11000
mvc:
servlet:
load-on-startup: 1
hiddenmethod:
filter:
enabled: true