我有一个Controller
类,它有一个将记录保存到数据库的函数。我正在传递几个参数给Controller
函数,但是我想我可能写错了@RequestMapping
。下面是代码
控制器
@RequestMapping(value="createRoadBlock.htm", method = RequestMethod.POST)
public @ResponseBody Integer createRoadBlock(@RequestParam String purpose, @RequestParam String userName,
@RequestParam int status, @RequestParam double latAdd,
@RequestParam double longAdd, HttpServletRequest request,
HttpServletResponse response) {
int roadBlockId = 0;
try{
roadBlockId = roadBlockManager.saveRoadBlock(purpose, userName, status,latAdd,longAdd);
logger.info("Create Road Block Successful roadBlockId "+ roadBlockId);
return roadBlockId;
}catch(Exception e){
logger.error("Exception Occured In Road Block Controller "+e.getMessage());
return roadBlockId;
}
}
** AJAX 请求**
$.ajax({
type:'POST',
url:'createRoadBlock.htm',
contentType:"application/json",
async:false,
cache:false,
data:{purpose:f_purpose, userName:f_userName,status: f_status,latAdd: f_latAdd, longAdd:f_lngAdd},
dataType:'json'
}).success(function(recordId){
console.log('Road Block created with id ' + recordId);
});
错误日志
Controller [com.crimetrack.web.RoadBlockController]
Method [public java.lang.Integer com.crimetrack.web.RoadBlockController.createRoadBlock(java.lang.String,java.lang.String,int,double,double,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'purpose' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:201)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:90)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
4条答案
按热度按时间nhjlsmyf1#
@PathVariable
用来告诉Spring,URI路径的一部分是您想要传递给方法的值。这是您想要的吗?或者变量应该是发布到URI的表单数据吗?如果需要表单数据,请使用
@RequestParam
而不是@PathVariable
。如果你想要
@PathVariable
,你需要在@RequestMapping
条目中指定占位符,告诉Spring路径变量在URI中的位置。例如,如果你想要提取一个名为contentId
的路径变量,你可以用途:@RequestMapping(value = "/whatever/{contentId}", method = RequestMethod.POST)
Edit:此外,如果您的路径变量可能包含一个.“”,而您想要该部分数据,那么您需要告诉Spring获取所有内容,而不仅仅是.“”之前的内容:
@RequestMapping(value = "/whatever/{contentId:.*}", method = RequestMethod.POST)
这是因为Spring的默认行为是将URL的该部分视为文件扩展名,并将其从变量提取中排除。
7ivaypg92#
我得到这个错误是因为一个愚蠢的错误,在@PathVariable中的变量名与在@RequestMapping中的变量名不匹配
比如说
它可能会帮助其他人
ego6inou3#
当您在REST定义中定义Map变量与@PathVariable名称不匹配时,可能会发生此错误
示例:假设您在REST定义中定义了
然后在定义函数时,应
当您使用REST控制器定义中未定义的任何其他变数搭配@PathVariable时,可能会发生此错误。例如,下列程式码会引发错误,因为ID与appId变数名称不同:
gxwragnw4#
我的问题是来自方法的参数的名称不同于在@GetMapping中传递的名称。它们应该相同,否则将出现这种无提示错误。