本次数据请求使用postman, postman下载地址:https://www.getpostman.com/
一、页面跳转
1. 页面跳转
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "/index";
}
}
2. 请求转发
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "/index";
}
@RequestMapping("/login")
public String forward(){
if(true){
return "forward:/index";
}
return "login";
}
}
3. 重定向
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "/index";
}
@RequestMapping("/login")
public String redirect(){
if(true){
return "redirect:/index";
}
return "login";
}
}
二、接收表单提交参数
1. 接收简单包装类型
模拟用户登录,接收用户名、密码、验证码参数
@PostMapping("/login1")
@ResponseBody
public Map<String, Object> submitLogin1(String username, String password, String captcha) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "登录成功");
resultMap.put("username", username);
resultMap.put("password", password);
resultMap.put("captcha", captcha);
return resultMap;
}
2. 通过request获取请求参数
@PostMapping("/login2")
@ResponseBody
public Map<String, Object> submitLogin2() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String username = request.getParameter("username");
String password = request.getParameter("password");
String captcha = request.getParameter("captcha");
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "登录成功");
resultMap.put("username", username);
resultMap.put("password", password);
resultMap.put("captcha", captcha);
return resultMap;
}
3. 接收对象类型
@Data
public class LoginUser {
private String username;
private String password;
private String code;
}
@PostMapping("/login3")
@ResponseBody
public Map<String, Object> submitLogin3(LoginUser loginUser) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "登录成功");
resultMap.put("username", loginUser.getUsername());
resultMap.put("password", loginUser.getPassword());
resultMap.put("captcha", loginUser.getCaptcha());
return resultMap;
}
4.接收Map类型
@PostMapping("/login4")
@ResponseBody
public Map<String, Object> submitLogin4(@RequestParam Map<String, Object> loginUser) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "登录成功");
resultMap.put("username", loginUser.get("username"));
resultMap.put("password", loginUser.get("password"));
resultMap.put("captcha", loginUser.get("captcha"));
return resultMap;
}
5.接收数组类型
@PostMapping("/modifyRole1")
@ResponseBody
public Map<String, Object> modifyRole1(Integer userId, Integer[] roleIds) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "修改角色权限成功");
resultMap.put("userId", userId);
resultMap.put("roleIds", Arrays.toString(roleIds));
return resultMap;
}
6.接收List类型
@PostMapping("/modifyRole2")
@ResponseBody
public Map<String, Object> modifyRole2(Integer userId, @RequestParam("roleIds") List<Integer> roleIds) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "修改角色权限成功");
resultMap.put("userId", userId);
resultMap.put("roleIds", roleIds.toString());
return resultMap;
}
7.接收Set类型
@PostMapping("/modifyRole3")
@ResponseBody
public Map<String, Object> modifyRole3(Integer userId, @RequestParam("roleIds") Set<String> roleIds) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "修改角色权限成功");
resultMap.put("userId", userId);
resultMap.put("roleIds", roleIds.toString());
return resultMap;
}
8.接收带List参数的实体类
@Data
public class ModifyRole {
private Integer userId;
private List<String> roleIds;
}
@PostMapping("/modifyRole4")
@ResponseBody
public Map<String, Object> modifyRole4(ModifyRole modifyRole) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "修改角色权限成功");
resultMap.put("modifyRole", modifyRole.toString());
return resultMap;
}
三、接收JSON参数
1. 接收带List参数的实体类
@Data
public class ModifyRole {
private Integer userId;
private List<String> roleIds;
}
@PostMapping("/modifyRole5")
@ResponseBody
public Map<String, Object> modifyRole5(@RequestBody ModifyRole modifyRole) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "修改角色权限成功");
resultMap.put("modifyRole", modifyRole.toString());
return resultMap;
}
2.接收List<Bean>类型
@Data
public class SysUser {
private String username;
private String password;
}
@PostMapping("/batchInsert")
@ResponseBody
public Map<String, Object> batchInsert(@RequestBody List<SysUser> sysUserList) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "批量新增用户成功");
resultMap.put("modifyRoleList", sysUserList.toString());
return resultMap;
}
3.接收Set<Bean>类型
@PostMapping("/batchInsert2")
@ResponseBody
public Map<String, Object> batchInsert2(@RequestBody Set<SysUser> sysUserSet) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("code", 200);
resultMap.put("msg", "批量新增用户成功");
resultMap.put("modifyRoleSet", sysUserSet.toString());
return resultMap;
}
四、文件上传、下载
1. 文件上传
新建一个文件
@PostMapping("/upload")
@ResponseBody
public void upload(MultipartFile multipartFile) throws IOException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// 获取文件流
InputStream inputStream = multipartFile.getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8.name()))){
// 读取文件数据
String collect = reader.lines().collect(Collectors.joining());
// 直接返回文件数据给前端
attributes.getResponse().getWriter().write(collect);
} catch (Exception e){
e.printStackTrace();
}
}
2. 文件下载
@GetMapping("/download")
public ResponseEntity<byte[]> download() throws IOException {
File file = ResourceUtils.getFile("classpath:application.yml");
byte[] body;
try (InputStream is = new FileInputStream(file)){
body = new byte[is.available()];
is.read(body);
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
return new ResponseEntity<>(body, headers, HttpStatus.OK);
}
到此这篇关于SpringMVC请求参数 的文章就介绍到这了,更多相关SpringMVC请求参数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!