一、问题描述
使用Springboot JPA 做分页查询,报错Required String parameter 'xx' is not present,后端未接受到请求
二、解决方案:
使用的请求方法是GetMapping,这时候传不了参数,需要改为PostMapping才会有效
错误源码:
注意注解:@GetMapping
package com.easylab.rentshop.controller; import com.easylab.rentshop.base.BaseResource;
import com.easylab.rentshop.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*; import java.util.Map; /******************************
* @author : liuyang
* <p>ProjectName:rent-shop </p>
* @ClassName : DepartmentController
* @date : 2018/6/13 0013
* @time : 15:31
* @createTime 2018-06-13 15:31
* @version : 2.0
* @description :
*
*
*
*******************************/ @RestController
@RequestMapping("department")
public class DepartmentController { @Autowired
private DepartmentService departmentService; /**
* @param pageStr
* @param pageSizeStr
* @return Page
* <p>
* <p>
* required和defaultValue设置当请求没有参数的时候,默认设置参数值为1
* 采用String类型接受,防止传入abc这种情况导致程序死掉
*/
@GetMapping("/departmentPage")
public Object departmentPage(@RequestParam(value = "pageStr", required = false, defaultValue = "1") String pageStr,
@RequestParam(value = "pageSizeStr") String pageSizeStr) { int pageNo = 1; try {
pageNo = Integer.valueOf(pageStr); if (pageNo < 1) {
pageNo = 1;
}
} catch (Exception e) {
} int pageSize = 0; try {
pageSize = Integer.valueOf(pageSizeStr); if (pageSize < 1) {
pageSize = 5;
}
} catch (Exception e) {
} Page page = departmentService.getPage(pageNo, pageSize); return new BaseResource(page);
} }
修改后:
package com.easylab.rentshop.controller; import com.easylab.rentshop.base.BaseResource;
import com.easylab.rentshop.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*; import java.util.Map; /******************************
* @author : liuyang
* <p>ProjectName:rent-shop </p>
* @ClassName : DepartmentController
* @date : 2018/6/13 0013
* @time : 15:31
* @createTime 2018-06-13 15:31
* @version : 2.0
* @description :
*
*
*
*******************************/ @RestController
@RequestMapping("department")
public class DepartmentController { @Autowired
private DepartmentService departmentService; /**
* @param pageStr
* @param pageSizeStr
* @return Page
* <p>
* <p>
* required和defaultValue设置当请求没有参数的时候,默认设置参数值为1
* 采用String类型接受,防止传入abc这种情况导致程序死掉
*/
@PostMapping("/departmentPage")
public Object departmentPage(@RequestParam(value = "pageStr", required = false, defaultValue = "1") String pageStr,
@RequestParam(value = "pageSizeStr") String pageSizeStr) { int pageNo = 1; try {
pageNo = Integer.valueOf(pageStr); if (pageNo < 1) {
pageNo = 1;
}
} catch (Exception e) {
} int pageSize = 0; try {
pageSize = Integer.valueOf(pageSizeStr); if (pageSize < 1) {
pageSize = 5;
}
} catch (Exception e) {
} Page page = departmentService.getPage(pageNo, pageSize); return new BaseResource(page);
} }
使用PostMan进行接口测试: