Required String parameter is not present

2017年06月28日 19:56:25

页面报Required String parameter ‘loanOrderNbr’ is not present解决办法

问题描述:

前面代码(angularjs)
var loanOrderNumber = $routeParams.loanOrderNumber;
    $scope.saveButton = true;
    $http.get("/test/business/order/viewDetail",{
        params : {
        'loanOrderNbr' : loanOrderNbr
        }
    }).success(function(request,status){ // }).error(function (data, status) { // });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
后台代码(spring mvc)
//查看数据
    @RequestMapping(value = "/viewDetail")
    @ResponseBody
    public AtResponse detail(@RequestParam(name = "loadOrderNbr", required = true) String loadOrderNbr) { Map params = new HashMap(); AtResponse resp = new AtResponse(System.currentTimeMillis()); //..... return resp; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
页面报错信息
:8080/test/business/order/viewDetail?loanOrderNbr=201706091042631 Failed to load resource: the server responded with a status of 400 (Bad Request)
  • 1

当时以为是前面angularjs代码写得有问题,然而仔细对比其他功能的写法,发现没有什么区别,只是传的参数不一样而矣,非常迷惑,不解。后台也不报错(没有输出spring日志),然后将请求链接直接在浏览器中访问: 
http://localhost:9770/test/business/order/viewDetail?loanOrderNbr=201706091042631

页面提示如下错误信息: 
Required String parameter ‘loanOrderNbr’ is not present

发现是前端传的参数名与后端代码定义的参数名不一致导致的,囧:

前端参数是:loanOrderNbr

后端参数是:loadOrderNbr

另:网上有另一解决方法参考:

将后台代码修改为:

@RequestMapping(value = "/viewDetail")      
@ResponseBody      
public ResponseBase addQuestion(@RequestBody Map params){ String loanOrderNbr = params.get("loanOrderNbr").toString(); }
  • 1
  • 2
  • 3
  • 4
  • 5
 

转载于:https://www.cnblogs.com/williamjie/p/9200634.html

你可能感兴趣的:(移动开发,java,后端)