小言_互联网的博客

SpringMVC请求参数示例

452人阅读  评论(0)

本次数据请求使用postman, postman下载地址:https://www.getpostman.com/

一、页面跳转

1. 页面跳转


  
  1. @Controller
  2. public class IndexController {
  3. /**
  4. * 进入首页
  5. *
  6. * @return 首页页面
  7. */
  8. @RequestMapping("/")
  9. public String index(){
  10. return "/index";
  11. }
  12. }

 

2. 请求转发


  
  1. @Controller
  2. public class IndexController {
  3. /**
  4. * 进入首页
  5. *
  6. * @return 首页页面
  7. */
  8. @RequestMapping("/")
  9. public String index(){
  10. return "/index";
  11. }
  12. /**
  13. * 访问登录页面时,如果已经登录就转发到首页,未登录就进入登录页面
  14. *
  15. * @return 登录页面
  16. */
  17. @RequestMapping("/login")
  18. public String forward(){
  19. if( true){
  20. return "forward:/index";
  21. }
  22. return "login";
  23. }
  24. }

 

3. 重定向


  
  1. @Controller
  2. public class IndexController {
  3. /**
  4. * 进入首页
  5. *
  6. * @return 首页页面
  7. */
  8. @RequestMapping("/")
  9. public String index(){
  10. return "/index";
  11. }
  12. /**
  13. * 访问登录页面时,如果已经登录就重定向到首页,未登录就进入登录页面
  14. *
  15. * @return 登录页面
  16. */
  17. @RequestMapping("/login")
  18. public String redirect(){
  19. if( true){
  20. return "redirect:/index";
  21. }
  22. return "login";
  23. }
  24. }

 

二、接收表单提交参数

1. 接收简单包装类型

模拟用户登录,接收用户名、密码、验证码参数


  
  1. /**
  2. * 提交登陆信息
  3. *
  4. * @param username 用户名
  5. * @param password 密码
  6. * @param captcha 验证码
  7. * @return 结果
  8. */
  9. @PostMapping("/login1")
  10. @ResponseBody
  11. public Map<String, Object> submitLogin1(String username, String password, String captcha) {
  12. Map<String, Object> resultMap = new HashMap<>( 16);
  13. resultMap.put( "code", 200);
  14. resultMap.put( "msg", "登录成功");
  15. resultMap.put( "username", username);
  16. resultMap.put( "password", password);
  17. resultMap.put( "captcha", captcha);
  18. return resultMap;
  19. }

 

2. 通过request获取请求参数


  
  1. /**
  2. * 提交登陆信息
  3. *
  4. * @return 结果
  5. */
  6. @PostMapping("/login2")
  7. @ResponseBody
  8. public Map<String, Object> submitLogin2() {
  9. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  10. HttpServletRequest request = attributes.getRequest();
  11. String username = request.getParameter( "username");
  12. String password = request.getParameter( "password");
  13. String captcha = request.getParameter( "captcha");
  14. Map<String, Object> resultMap = new HashMap<>( 16);
  15. resultMap.put( "code", 200);
  16. resultMap.put( "msg", "登录成功");
  17. resultMap.put( "username", username);
  18. resultMap.put( "password", password);
  19. resultMap.put( "captcha", captcha);
  20. return resultMap;
  21. }

 

3. 接收对象类型


  
  1. @Data
  2. public class LoginUser {
  3. private String username;
  4. private String password;
  5. private String code;
  6. }

  
  1. /**
  2. * 提交登陆信息
  3. *
  4. * @return 结果
  5. */
  6. @PostMapping("/login3")
  7. @ResponseBody
  8. public Map<String, Object> submitLogin3(LoginUser loginUser) {
  9. Map<String, Object> resultMap = new HashMap<>( 16);
  10. resultMap.put( "code", 200);
  11. resultMap.put( "msg", "登录成功");
  12. resultMap.put( "username", loginUser.getUsername());
  13. resultMap.put( "password", loginUser.getPassword());
  14. resultMap.put( "captcha", loginUser.getCaptcha());
  15. return resultMap;
  16. }

 

4.接收Map类型


  
  1. /**
  2. * 提交登陆信息
  3. *
  4. * @return 结果
  5. */
  6. @PostMapping("/login4")
  7. @ResponseBody
  8. public Map<String, Object> submitLogin4(@RequestParam Map<String, Object> loginUser) {
  9. Map<String, Object> resultMap = new HashMap<>( 16);
  10. resultMap.put( "code", 200);
  11. resultMap.put( "msg", "登录成功");
  12. resultMap.put( "username", loginUser.get( "username"));
  13. resultMap.put( "password", loginUser.get( "password"));
  14. resultMap.put( "captcha", loginUser.get( "captcha"));
  15. return resultMap;
  16. }

 

5.接收数组类型


  
  1. /**
  2. * 修改角色权限
  3. *
  4. * @param userId 用户id
  5. * @param roleIds 角色id
  6. * @return 结果
  7. */
  8. @PostMapping("/modifyRole1")
  9. @ResponseBody
  10. public Map<String, Object> modifyRole1(Integer userId, Integer[] roleIds) {
  11. Map<String, Object> resultMap = new HashMap<>( 16);
  12. resultMap.put( "code", 200);
  13. resultMap.put( "msg", "修改角色权限成功");
  14. resultMap.put( "userId", userId);
  15. resultMap.put( "roleIds", Arrays.toString(roleIds));
  16. return resultMap;
  17. }

 

6.接收List类型


  
  1. /**
  2. * 修改角色权限
  3. *
  4. * @param userId 用户id
  5. * @param roleIds 角色id
  6. * @return 结果
  7. */
  8. @PostMapping("/modifyRole2")
  9. @ResponseBody
  10. public Map<String, Object> modifyRole2(Integer userId, @RequestParam("roleIds") List<Integer> roleIds) {
  11. Map<String, Object> resultMap = new HashMap<>( 16);
  12. resultMap.put( "code", 200);
  13. resultMap.put( "msg", "修改角色权限成功");
  14. resultMap.put( "userId", userId);
  15. resultMap.put( "roleIds", roleIds.toString());
  16. return resultMap;
  17. }

 

7.接收Set类型


  
  1. /**
  2. * 修改角色权限
  3. *
  4. * @param userId 用户id
  5. * @param roleIds 角色id
  6. * @return 结果
  7. */
  8. @PostMapping("/modifyRole3")
  9. @ResponseBody
  10. public Map<String, Object> modifyRole3(Integer userId, @RequestParam("roleIds") Set<String> roleIds) {
  11. Map<String, Object> resultMap = new HashMap<>( 16);
  12. resultMap.put( "code", 200);
  13. resultMap.put( "msg", "修改角色权限成功");
  14. resultMap.put( "userId", userId);
  15. resultMap.put( "roleIds", roleIds.toString());
  16. return resultMap;
  17. }

 

8.接收带List参数的实体类


  
  1. @Data
  2. public class ModifyRole {
  3. private Integer userId;
  4. private List<String> roleIds;
  5. }

  
  1. /**
  2. * 修改角色权限
  3. *
  4. * @param modifyRole 数据
  5. * @return 结果
  6. */
  7. @PostMapping("/modifyRole4")
  8. @ResponseBody
  9. public Map<String, Object> modifyRole4(ModifyRole modifyRole) {
  10. Map<String, Object> resultMap = new HashMap<>( 16);
  11. resultMap.put( "code", 200);
  12. resultMap.put( "msg", "修改角色权限成功");
  13. resultMap.put( "modifyRole", modifyRole.toString());
  14. return resultMap;
  15. }

 

三、接收JSON参数

1. 接收带List参数的实体类


  
  1. @Data
  2. public class ModifyRole {
  3. private Integer userId;
  4. private List<String> roleIds;
  5. }

  
  1. /**
  2. * 修改角色权限
  3. *
  4. * @param modifyRole 数据
  5. * @return 结果
  6. */
  7. @PostMapping("/modifyRole5")
  8. @ResponseBody
  9. public Map<String, Object> modifyRole5(@RequestBody ModifyRole modifyRole) {
  10. Map<String, Object> resultMap = new HashMap<>( 16);
  11. resultMap.put( "code", 200);
  12. resultMap.put( "msg", "修改角色权限成功");
  13. resultMap.put( "modifyRole", modifyRole.toString());
  14. return resultMap;
  15. }

 

2.接收List<Bean>类型


  
  1. @Data
  2. public class SysUser {
  3. private String username;
  4. private String password;
  5. }

  
  1. /**
  2. * 批量新增用户
  3. *
  4. * @param sysUserList 数据集合
  5. * @return 结果
  6. */
  7. @PostMapping("/batchInsert")
  8. @ResponseBody
  9. public Map<String, Object> batchInsert(@RequestBody List<SysUser> sysUserList) {
  10. Map<String, Object> resultMap = new HashMap<>( 16);
  11. resultMap.put( "code", 200);
  12. resultMap.put( "msg", "批量新增用户成功");
  13. resultMap.put( "modifyRoleList", sysUserList.toString());
  14. return resultMap;
  15. }

 

3.接收Set<Bean>类型


  
  1. /**
  2. * 批量新增用户
  3. *
  4. * @param sysUserSet 数据集合
  5. * @return 结果
  6. */
  7. @PostMapping("/batchInsert2")
  8. @ResponseBody
  9. public Map<String, Object> batchInsert2(@RequestBody Set<SysUser> sysUserSet) {
  10. Map<String, Object> resultMap = new HashMap<>( 16);
  11. resultMap.put( "code", 200);
  12. resultMap.put( "msg", "批量新增用户成功");
  13. resultMap.put( "modifyRoleSet", sysUserSet.toString());
  14. return resultMap;
  15. }

 

四、文件上传、下载

1. 文件上传

新建一个文件


  
  1. /**
  2. * 上传文件
  3. *
  4. * @param multipartFile 上传的文件
  5. * @return 结果
  6. */
  7. @PostMapping("/upload")
  8. @ResponseBody
  9. public void upload(MultipartFile multipartFile) throws IOException {
  10. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  11. // 获取文件流
  12. InputStream inputStream = multipartFile.getInputStream();
  13. try (BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream, UTF_8.name()))){
  14. // 读取文件数据
  15. String collect = reader.lines().collect(Collectors.joining());
  16. // 直接返回文件数据给前端
  17. attributes.getResponse().getWriter().write(collect);
  18. } catch (Exception e){
  19. e.printStackTrace();
  20. }
  21. }

 

2. 文件下载


  
  1. /**
  2. * 文件下载
  3. */
  4. @GetMapping("/download")
  5. public ResponseEntity< byte[]> download() throws IOException {
  6. File file = ResourceUtils.getFile( "classpath:application.yml");
  7. byte[] body;
  8. try (InputStream is = new FileInputStream(file)){
  9. body = new byte[is.available()];
  10. is.read(body);
  11. }
  12. HttpHeaders headers = new HttpHeaders();
  13. headers.add( "Content-Disposition", "attchement;filename=" + file.getName());
  14. return new ResponseEntity<>(body, headers, HttpStatus.OK);
  15. }

 


转载:https://blog.csdn.net/han12398766/article/details/117389740
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场