网站开发与客户沟通,北京梵客装饰,自己的简历网站怎么做,推广产品最好的方式目录 一、引子
二、注解解析
RequestParam
一、要求形参名请求参数名#xff0c;或者是请求实体类时#xff08;已有实体类#xff09;#xff0c;可以不需要加该注解
二、请求参数名!参数名时#xff0c;需要写该注解RequestParam#xff0c;其中
三、一名多值的情…目录 一、引子
二、注解解析
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“指定请求参数名”
requiredfalse前端是否必须传递此参数默认是必须不传报错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 ListString 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如PostMappingDeleteMappingPutMapping中有时会用到
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;}}