Java 三个参数中有两个未到达弹簧控制器
我的Spring4控制器中有以下内容。我正在尝试将记录添加到tablein数据库中。 我不确定为什么我使用POSTMAN客户端提供的参数Java 三个参数中有两个未到达弹簧控制器,java,spring,spring-4,Java,Spring,Spring 4,我的Spring4控制器中有以下内容。我正在尝试将记录添加到tablein数据库中。 我不确定为什么我使用POSTMAN客户端提供的参数testingID\uucode>和definition没有到达控制器。 但是,参数type_id正在到达,如下所述 从以下事实可以看出这一点: 当我使用http://localhost:8080/MyProject/addEmployeeRecord?employee_id=1234&type_id=4444&definition=test1212 1) 我检
testingID\uucode>和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\uuu
始终是0
,这是默认值,小于1
,因此我总是得到无效的员工ID参数
异常。如何对此进行故障排除
@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;
}
@RequestMapping(value=“/addEmployeeRecord”,method=RequestMethod.GET)
公共字符串addEmployeeRecord
(
@RequestParam(value=“employee\u id”,defaultValue=“0”)整数测试id,
@RequestParam(value=“type\u id”,defaultValue=“0”)整数类型\u id,
@RequestParam(value=“value”,defaultValue=“”)字符串定义,
@RequestParam(value=“rank”,defaultValue=“1”)整数秩,
HttpServletRequest请求
)
{
字符串wsStatusJsonString=null;
logger.info(“-----------------------------------------------------------------------------------------------------------------------------------”;
logger.info(“HttpRequest参数检查”);
request.getParameterMap().entrySet().forEach(entry->logger.info(“参数:”+entry.getKey()+Arrays.toString(entry.getValue())));
logger.info(“addEmployeeRecord ws参数:”+
“value=“+定义+
“,employee_id=“+testingID_+
,type_id=“+type_id+
“,rank=“+rank
);
布尔addStatus=true;
试一试{
//验证输入参数。
如果(testingID_u<1){抛出新异常(“无效的员工ID参数”);}
如果(StringUtils.isNullOrEmpty(定义)){抛出新异常(“无效的定义参数”);}
//一些代码
logger.info(“addEmployeeRecord状态:“+addStatus”);
如果(添加状态){
wsStatusJsonString=GenericOrmStatusView.OrmStatusToJsonString(true,“成功插入员工记录!”,true);
}否则{
wsStatusJsonString=GenericOrmStatusView.OrmStatusToJsonString(true,“错误!无法插入员工记录!”,true);
}
}捕获(可丢弃){
th.printStackTrace();
wsStatusJsonString=GenericOrmStatusView.OrmStatusToJsonString(false,th.getMessage(),true);
}
logger.info(“-----------------------------------------------------------------------------------------------------------------------------------”;
返回wsStatusJsonString;
}
在控制器方法中,声明了@RequestParam(value=“value”,defaultValue=“”)字符串定义
,而您将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;
}
@RequestMapping(value=“/addEmployeeRecord”,method=RequestMethod.GET)
公共字符串addEmployeeRecord(@RequestParam(value=“employee\u id”,defaultValue=“0”)整数testingID,
@RequestParam(value=“type\u id”,defaultValue=“0”)整数类型\u id,
@RequestParam(value=“value”,defaultValue=“”)字符串定义,
@RequestParam(value=“rank”,defaultValue=“1”)整数秩,HttpServletRequest请求
) {
试一试{
//验证输入参数。
如果(测试ID_uu1){
抛出新异常(“无效的员工ID参数”);
}
if(StringUtils.isEmpty(定义)){
抛出新异常(“定义参数无效”);
}
//一些代码
}捕获(可丢弃){
th.printStackTrace();
}
返回“testingID”+testingID+“type\u id-”+type\u id+“definition”+definition+“rank”+rank;
}
邮递员截图为:
@DwB我到底在哪里丢失了&
?我尝试将其更改为值。仍然出现相同的错误。我运行了您的代码并使用postman进行了测试-http://localhost:8080/mappings/addEmployeeRecord?employee_id=1234&type_id=4444&value=test1212
我添加了一个return语句作为return“testingID”+testingID+“type\u id-”+type\u id+“definition”+definition+“rank”+rank代码>返回的响应是testingID\uu1234 type\u id-4444 definition test1212 rank 1
Hmm,我不知道为什么它在我这边不起作用。您认为我应该尝试更改tomcat容器吗?我使用SpringBoot测试您的代码,因此我嵌入了tomcat来测试您的代码。嗯,我将尝试将webservice端点的位置更改为某个不同的控制器java类,并查看它在那里的行为。您还推荐其他故障排除步骤吗?