三分之二的参数未到达 Spring 控制器
Two parameters out of three not reaching the Spring controller
我的 Spring 4 控制器中有以下内容。我正在尝试将记录添加到表数据库中。
我不确定为什么我使用 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;
}
在您的控制器方法中,声明了 @RequestParam(value="value", defaultValue="") String definition
,而您将 definition=test1212
作为查询参数传递,要传递定义,您需要改为设置 value=test1212
。我使用下面的代码来测试请求参数:
@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;
}
邮递员捕获是:
我的 Spring 4 控制器中有以下内容。我正在尝试将记录添加到表数据库中。
我不确定为什么我使用 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;
}
在您的控制器方法中,声明了 @RequestParam(value="value", defaultValue="") String definition
,而您将 definition=test1212
作为查询参数传递,要传递定义,您需要改为设置 value=test1212
。我使用下面的代码来测试请求参数:
@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;
}
邮递员捕获是: