zoukankan      html  css  js  c++  java
  • Jpa 报错 :HTTP Status 400

    一、问题描述 

              使用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 }
    View Code

    修改后:

     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 }
    View Code

    使用PostMan进行接口测试:

  • 相关阅读:
    python的filter函数的使用方法详解以及使用案例,是否以什么结尾,是否大于什么(判断是True,则留下来)
    python的reduce函数的使用方法详解以及使用案例,相加,相乘(处理一个序列,然后把序列进程合并操作)
    python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)
    python的匿名函数 lambda的使用方法详解以及使用案例
    python函数的 全局变量与局部变量
    python的函数介绍 位置参数 关键字参数 默认参数 参数组 *args **kwargs
    python set() 集合的添加删除、交集、并集、差集、交叉补集、集合的方法介绍以及使用案例
    python的dict()字典数据类型的方法详解以及案例使用
    python的tuple()元组数据类型的使用方法以及案例
    MATLAB分类与预测算法函数
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/9178664.html
Copyright © 2011-2022 走看看