SpringMVC五种类型参数传递和json数据传递参数

举报
不会压弯的小飞侠 发表于 2022/08/08 22:57:29 2022/08/08
【摘要】 🍁博客主页:👉不会压弯的小飞侠 ✨欢迎关注:👉点赞👍收藏⭐留言✒ ✨系列专栏:👉SpringMVC注解开发 ✨如果觉得博主的文章还不错的话,请三连支持一下博主。 🔥欢迎大佬指正,一起...

在这里插入图片描述

🍁博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉SpringMVC注解开发
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!

在这里插入图片描述

学习内容:

  • 1.普通参数
    • url地址传参,地址参数名与形参变量名相同,定义形参即可接收参数

    • 请求参数名与形参变量名不同,使用@RequestParam绑定参数

      • @RequestParam
      • 类型:形参注解
      • 位置: SpringMVC控制器方法形参定义前面
      • 作用:绑定请求参数与处理器方法形参间的关系
      • 参数:
        • required:是否为必传参数
        • defaultValue:参数默认值
@RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(@RequestParam("name")String username,int age){
        System.out.println("普通参数传递name:"+username);
        System.out.println("普通参数传递age:"+age);
        return "'module':'common param'";
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 2.pojo参数
    • 请求参数名与形参对象属性名相同,定义pojo类型形参即可接收参数
  • 3.嵌套pojo
    • 请求参数名与形参对象属性名相同,按照对象层次结构关系即可接收嵌套pojo属性参数
  • 4.数组参数
    • 请求参数名与形参对象属性名相同且请求参数有多个,定义数组类型形参即可接收参数
  • 5.集合参数
    • 请求参数名与形参对象属性名相同且请求参数有多个,使用@RequestParam绑定参数
  • 6.解决中文乱码
    • Post请求中文乱码处理
    • 为web容器添加过滤器并指定字符集,Spring-web包中提供了专用的字符过滤器
   protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 7.json数据传递参数
  • @EnablewebMvc
    • 类型:配置类注解
    • 位置: SpringMVc配置类定义上方
    • 作用:开启SpringMVC多项辅助功能, 开启自动转换json数据的支持,由json数据转换为对象的功能
@Configuration
@ComponentScan("com.study.controller")
@EnablewebMvc
public class SpringMvcConfig {
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • @RequestBody
    • 类型:形参注解
    • 位置:SpringMVC控制器方法形参定义前面
    • 作用:将请求中请求体所包含的数据传递给请求参数,此注解一个处理器方法只能使用一次
 @RequestMapping("/pojos")
    @ResponseBody
    public String pojosParam(@RequestBody User user){
        System.out.println("pojos参数传递user:"+user);
        return "'module':'pojos.....'";
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

案例分析:

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>SpringMVC-Demo1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.22.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
     <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.3</version>
    </dependency>
  </dependencies>
</project>


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

2.ServletContainersInitConfig

package com.study.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}
/*
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载SpringMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }
    //设置哪些请求可以归属SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //加载Spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class);
        return ctx;
    }
}*/


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

3.SpringMvcConfig

package com.study.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.study.controller")
public class SpringMvcConfig {
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.UserController

package com.study.controller;

import com.study.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
public class UserController {
    //1.普通参数传递
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name,int age){
        System.out.println("普通参数传递name:"+name);
        System.out.println("普通参数传递age:"+age);
        return "'module':'common param'";
        //http://localhost:8080/SpringMVC_Demo3/commonParam?name=tom&age=13  普通参数传递name:jack  普通参数传递age:13
    }
    //2.pojo参数传递
    @RequestMapping("/pojo")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo参数传递user:"+user);
        return "'module':'pojo.....'";
        //http://localhost:8080/SpringMVC_Demo3/pojo?name=%E5%B0%8F%E9%A3%9E%E4%BE%A0&age=13
     //pojo参数传递user:User{name='小飞侠', age=13}
    }
    // 3.嵌套pojo参数
    @RequestMapping("/pojos")
    @ResponseBody
    public String pojosParam(User user){
        System.out.println("pojos参数传递user:"+user);
        return "'module':'pojos.....'";
        //http://localhost:8080/SpringMVC_Demo3/pojos?name=%E5%B0%8F%E9%A9%AC%E5%93%A5&age=13&address.province=%E6%B2%B3%E5%8D%97&address.city=%E5%95%86%E4%B8%98
        //pojo参数传递user:User{name='小马哥', age=13, address=Address{province='河南', city='商丘'}}
    }
    //4.数组参数传递
    @RequestMapping("/arr")
    @ResponseBody
    public String array(String [] arr){
        System.out.println("数组参数传递arr:"+ Arrays.toString(arr));
        return "'module':'array.....'";
        //http://localhost:8080/SpringMVC_Demo3/arr?arr=1&arr=2&arr=3
        //数组参数传递arr:[1, 2, 3]
    }
    //5.集合参数
    @RequestMapping("/coll")
    @ResponseBody
    public String collection(@RequestParam List<String> colls){
        System.out.println("集合参数传递colls:"+colls);
        return "'module':'collection.....'";
        //http://localhost:8080/SpringMVC_Demo3/coll?colls=1&colls=2&colls=3
        //集合参数传递colls:[1, 2, 3]
    }
     //6.pojo参数:json格式
    @RequestMapping("/json")
    @ResponseBody
    public String json(User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        System.out.println(json);
        return "'module':'json.....'";
        //http://localhost:8080/SpringMVC_Demo3/json?name=小马哥&age=23&address.province=河南&address.city=商丘
        //{"name":"小马哥","age":23,"address":{"province":"河南","city":"商丘"}}
    }

}




  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

5.Address

package com.study.domain;

public class Address {
    private String province;
    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

6.User

package com.study.domain;

public class User {
    private String name;
    private int age;

    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_43514330/article/details/125578156

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。