三个中的两个参数未到达Spring控制器

时间:2017-12-12 21:36:41

标签: java spring spring-4

我的Spring 4控制器中有以下内容。我正在尝试将记录添加到tablein数据库中。 我不确定为什么我使用POSTMAN客户端提供的参数testingID_definition没有到达控制器。 但是,参数type_id正在达到,如下所述。

从以下事实可以看出这一点:

当我使用http://localhost:8080/MyProject/addEmployeeRecord?employee_id=1234&type_id=4444&definition=test1212

1)我检查了控制台输出以查找以下行:

logger.info("addEmployeeRecord ws params : " + 

                            "value="+definition+
                            ",employee_id="+testingID_+
                            ",type_id="+type_id+
                            ",rank="+rank
                            );

我在控制台日志中注意到以下内容:

addEmployeeRecord ws params : value=,employee_id=0,type_id=4444,rank=1

2)我通过注入HttpServletRequest进行检查,如下所示,当我看到以下行时,我看到了所有参数 执行:

request.getParameterMap().entrySet().forEach(entry -> logger.info("Parameter: " + entry.getKey() + Arrays.toString(entry.getValue())));

结果如下:

Parameter: employee_id[1234]
Parameter: type_id[4444]
Parameter: definition[test1212]

由于testingID_始终是0,这是默认值且小于1,因此我总是会遇到Invalid Employee ID parameter异常。我该如何解决这个问题?

@RequestMapping(value="/addEmployeeRecord", method=RequestMethod.GET)
                public String addEmployeeRecord
                (
                    @RequestParam(value="employee_id", defaultValue="0") Integer testingID_,
                    @RequestParam(value="type_id", defaultValue="0") Integer type_id,  
                    @RequestParam(value="value", defaultValue="") String definition,
                    @RequestParam(value="rank", defaultValue="1") Integer rank,
                    HttpServletRequest request

                ) 
                {

                    String wsStatusJsonString = null;

                    logger.info("-----------------------------------------------------------------------------------------------------------");
                    logger.info("HttpRequest Parameters Check");
                    request.getParameterMap().entrySet().forEach(entry -> logger.info("Parameter: " + entry.getKey() + Arrays.toString(entry.getValue())));
                    logger.info("addEmployeeRecord ws params : " + 

                            "value="+definition+
                            ",employee_id="+testingID_+
                            ",type_id="+type_id+
                            ",rank="+rank
                            );

                    boolean addStatus = true;       
                    try {

                        // Validate the input parameters.
                        if (testingID_ < 1 ) {throw new Exception("Invalid Employee ID parameter"); }
                        if (StringUtils.isNullOrEmpty(definition)) { throw new Exception("Invalid  Definition Parameter");}

                        //some code


                        logger.info("addEmployeeRecord status:" + addStatus);
                        if (addStatus) {
                            wsStatusJsonString = GenericOrmStatusView.OrmStatusToJsonString(true, "Successfully Inserted Employee Record!", true);
                        } else {
                            wsStatusJsonString = GenericOrmStatusView.OrmStatusToJsonString(true, "Error ! Unable to Insert Employee Record !", true);
                        }
                    } catch (Throwable th) {
                        th.printStackTrace();
                        wsStatusJsonString = GenericOrmStatusView.OrmStatusToJsonString(false, th.getMessage(), true);
                    }

                    logger.info("-----------------------------------------------------------------------------------------------------------");
                    return wsStatusJsonString;
                }

1 个答案:

答案 0 :(得分:1)

在您的控制器方法中,声明@RequestParam(value="value", defaultValue="") String definition,而您传递definition=test1212作为查询参数,以传递您需要设置value=test1212的定义。我使用下面的代码来测试req参数:

@RequestMapping(value = "/addEmployeeRecord", method = RequestMethod.GET)
    public String addEmployeeRecord(@RequestParam(value = "employee_id", defaultValue = "0") Integer testingID_,
            @RequestParam(value = "type_id", defaultValue = "0") Integer type_id,
            @RequestParam(value = "value", defaultValue = "") String definition,
            @RequestParam(value = "rank", defaultValue = "1") Integer rank, HttpServletRequest request

    ) {

        try {

            // Validate the input parameters.
            if (testingID_ < 1) {
                throw new Exception("Invalid Employee ID parameter");
            }
            if (StringUtils.isEmpty(definition)) {
                throw new Exception("Invalid  Definition Parameter");
            }

            // some code

        } catch (Throwable th) {
            th.printStackTrace();
        }

        return "testingID_ " + testingID_ + " type_id- " + type_id + " definition " + definition + " rank " + rank;
    }

邮差捕获是:

Postman Request and Response