栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 animal;
    private Map score;
    private Set salaries;
    private Map> 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 getAnimal() {
        return animal;
    }

    public void setAnimal(List animal) {
        this.animal = animal;
    }

    public Map getScore() {
        return score;
    }

    public void setScore(Map score) {
        this.score = score;
    }

    public Set getSalaries() {
        return salaries;
    }

    public void setSalaries(Set salaries) {
        this.salaries = salaries;
    }

    public Map> getAllPets() {
        return allPets;
    }

    public void setAllPets(Map> 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 getCar(
            @PathVariable("id") Integer id,
            @PathVariable("username") String name,
            Map pv,
            @RequestHeader("User-Agent") String userAgent,
            @RequestHeader Map head,
            @RequestParam("age") Integer age,
            @RequestParam("inters") List inters,
            @RequestParam Map params,
            @cookievalue("_ga") String _ga) {
        Map 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 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 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755343.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号