一、問題描述
使用Springboot JPA 做分頁查詢,報錯Required String parameter 'xx' is not present,后端未接受到請求
二、解決方案:
使用的請求方法是GetMapping,這時候傳不了參數,需要改為PostMapping才會有效
錯誤源碼:
注意注解:@GetMapping

1 package com.easylab.rentshop.controller; 2 3 import com.easylab.rentshop.base.BaseResource; 4 import com.easylab.rentshop.service.DepartmentService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.data.domain.Page; 7 import org.springframework.web.bind.annotation.*; 8 9 import java.util.Map; 10 11 /****************************** 12 * @author : liuyang 13 * <p>ProjectName:rent-shop </p> 14 * @ClassName : DepartmentController 15 * @date : 2018/6/13 0013 16 * @time : 15:31 17 * @createTime 2018-06-13 15:31 18 * @version : 2.0 19 * @description : 20 * 21 * 22 * 23 *******************************/ 24 25 @RestController 26 @RequestMapping("department") 27 public class DepartmentController { 28 29 30 @Autowired 31 private DepartmentService departmentService; 32 33 34 /** 35 * @param pageStr 36 * @param pageSizeStr 37 * @return Page 38 * <p> 39 * <p> 40 * required和defaultValue設置當請求沒有參數的時候,默認設置參數值為1 41 * 采用String類型接受,防止傳入abc這種情況導致程序死掉 42 */ 43 @GetMapping("/departmentPage") 44 public Object departmentPage(@RequestParam(value = "pageStr", required = false, defaultValue = "1") String pageStr, 45 @RequestParam(value = "pageSizeStr") String pageSizeStr) { 46 47 int pageNo = 1; 48 49 try { 50 pageNo = Integer.valueOf(pageStr); 51 52 if (pageNo < 1) { 53 pageNo = 1; 54 } 55 } catch (Exception e) { 56 } 57 58 int pageSize = 0; 59 60 try { 61 pageSize = Integer.valueOf(pageSizeStr); 62 63 if (pageSize < 1) { 64 pageSize = 5; 65 } 66 } catch (Exception e) { 67 } 68 69 Page page = departmentService.getPage(pageNo, pageSize); 70 71 return new BaseResource(page); 72 } 73 74 75 }
修改后:

1 package com.easylab.rentshop.controller; 2 3 import com.easylab.rentshop.base.BaseResource; 4 import com.easylab.rentshop.service.DepartmentService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.data.domain.Page; 7 import org.springframework.web.bind.annotation.*; 8 9 import java.util.Map; 10 11 /****************************** 12 * @author : liuyang 13 * <p>ProjectName:rent-shop </p> 14 * @ClassName : DepartmentController 15 * @date : 2018/6/13 0013 16 * @time : 15:31 17 * @createTime 2018-06-13 15:31 18 * @version : 2.0 19 * @description : 20 * 21 * 22 * 23 *******************************/ 24 25 @RestController 26 @RequestMapping("department") 27 public class DepartmentController { 28 29 30 @Autowired 31 private DepartmentService departmentService; 32 33 34 /** 35 * @param pageStr 36 * @param pageSizeStr 37 * @return Page 38 * <p> 39 * <p> 40 * required和defaultValue設置當請求沒有參數的時候,默認設置參數值為1 41 * 采用String類型接受,防止傳入abc這種情況導致程序死掉 42 */ 43 @PostMapping("/departmentPage") 44 public Object departmentPage(@RequestParam(value = "pageStr", required = false, defaultValue = "1") String pageStr, 45 @RequestParam(value = "pageSizeStr") String pageSizeStr) { 46 47 int pageNo = 1; 48 49 try { 50 pageNo = Integer.valueOf(pageStr); 51 52 if (pageNo < 1) { 53 pageNo = 1; 54 } 55 } catch (Exception e) { 56 } 57 58 int pageSize = 0; 59 60 try { 61 pageSize = Integer.valueOf(pageSizeStr); 62 63 if (pageSize < 1) { 64 pageSize = 5; 65 } 66 } catch (Exception e) { 67 } 68 69 Page page = departmentService.getPage(pageNo, pageSize); 70 71 return new BaseResource(page); 72 } 73 74 75 }
使用PostMan進行接口測試: