spring-boot基本项目(请求转发,请求参数,application.yml配置,Rest映射)

这里是结构

在这里插入图片描述

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") //跟配置文件里的person绑定
@Component // 指定为spring容器中的组件
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";
    }

    //    同一个url
    @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("id",id);
//        map.put("name",name);
//        map.put("pv",pv);
//        map.put("userAgent",userAgent);
//        map.put("headers",head);
        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 {

//    访问goto会转发到success
    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        request.setAttribute("msg","成功了···");
        request.setAttribute("code",200);
        return "forward:success"; // 转发到 /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: [篮球,足球]
  interests:
    - 篮球
    - 足球
    - 18
  animal: [ 阿猫,阿狗 ]
  #  score:
  #    english: 90
  #    math: 90
  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
spring:
  banner:
    image:
      location:
      bitdepth: 4
  cache:
    type: redis
    redis:
      time-to-live: 11000
    # 配置web
    resources: # 自定义静态资源目录
      static-locations:
        [ classpath: /custom/ ]
      add-mappings: true
      cache:
        period: 11000
  # 配置mvc
  mvc:
    servlet:
      load-on-startup: 1  # 初始化dispatcherServlet(默认-1未开启)
    #      static-path-pattern: /res/** # 给静态资源路径添加前缀
    hiddenmethod:
      filter:
        enabled: true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修罗_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值