SpringMVC中接收参数总结

目录

一、引子

二、注解解析

@RequestParam

一、要求形参名=请求参数名,或者是请求实体类时(已有实体类),可以不需要加该注解

二、请求参数名!=参数名时,需要写该注解@RequestParam,其中

三、一名多值的情况使用list时,需要用到该注解@RequestParam,将集合add加入对应数据类型

@PathVariable

@RequestBody

@RequestHeader


一、引子

最近在写项目时,时不时会忘记或者漏写对应的参数注解,所以决定简单整理一下相关注解。

二、注解解析

@RequestParam

一、要求形参名=请求参数名,或者是请求实体类时(已有实体类),可以不需要加该注解

package com.atguigu.param;

import com.atguigu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {

    @RequestMapping("data")
    @ResponseBody
    //直接接收
    //要求请求参数名=形参名
    public String data(String name,int age){

        System.out.println("name = " + name + ", age = " + age);
        return "name = " + name + ", age = " + age;

    }

    @GetMapping("data3")
    @ResponseBody
    //使用实体对象接值
    public String data3(User user){
        System.out.println("user = " + user);
        return "user = " + user;
    }

}

二、请求参数名!=参数名时,需要写该注解@RequestParam,其中

value=“指定请求参数名”

required=false,前端是否必须传递此参数,默认是必须,不传报错400

default=“1”,当非必须传递false,可以设默认值

package com.atguigu.param;

import com.atguigu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {

    @GetMapping("data1")
    @ResponseBody
    //注解指定
    public String data1(@RequestParam(value = "account") String username,
                        @RequestParam(required = false,defaultValue = "1") int page){
        System.out.println("username = " + username + ", page = " + page);
        return "username = " + username + ", page = " + page;
    }


}

三、一名多值的情况使用list时,需要用到该注解@RequestParam,将集合add加入对应数据类型

如果不加该注解,将会让hbs对应的一个字符串直接赋值给集合。

package com.atguigu.param;

import com.atguigu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {

    @GetMapping("data2")
    @ResponseBody
    //特殊值一名多值
    public String data2(@RequestParam List hbs){
        System.out.println("hbs = " + hbs);
        return "hbs = " + hbs;
    }

}

@PathVariable

路径传参时,必须用到@PathVariable

package com.atguigu.path;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("path")
@ResponseBody
public class PathController {
    @GetMapping("{account}/{password}")
    public String login(@PathVariable String account,@PathVariable String password){

        System.out.println("account = " + account + ", password = " + password);
        return "account = " + account + ", password = " + password;

    }

}

@RequestBody

前端传入json数据时需要用到@RequestBody,如@PostMapping,@DeleteMapping,@PutMapping中有时会用到

package com.atguigu.json;

import com.atguigu.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("json")
@ResponseBody
public class JsonController {

    @PostMapping("data")
    public String data(@RequestBody Person person){

        System.out.println("person = " + person);
        return "person = " + person;

    }

}

@RequestHeader

用于接收请求头

package com.atguigu.header;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("header")
@ResponseBody
public class HeaderController {

    @RequestMapping ("data")
    public String data(@RequestHeader String host){
        System.out.println("host = " + host);
        return "host = " + host;
    }

}

你可能感兴趣的:(SSM,java,spring,spring,boot)