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进行接口测试:

  • 相关阅读:
    0.Mysql优化框架
    [C]extern char **environ
    [C]Armstrong Number
    [C]struct, union的结合
    [C]enum类型
    [C]fibonacci series, recursion, time.h
    [C/JAVA]二维数组
    [C] char**, swich-case, refactoring, stdbool.h
    hardly/scarcely ... goes by without ...
    [C]随机数生成
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/9178664.html
Copyright © 2011-2022 走看看