弹簧 MVC 缺少 URI 模板变量

我有一个类,其函数将记录保存到数据库中。我正在将几个参数传递给函数,但我认为我可能写错了。下方是代码ControllerController@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({
    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)

答案 1

@PathVariable用于告诉 Spring,URI 路径的一部分是要传递给方法的值。这是您想要的,还是变量应该是发布到URI的表单数据?

如果需要表单数据,请使用 代替 。@RequestParam@PathVariable

如果需要,则需要在条目中指定占位符,以告知 Spring 路径变量在 URI 中的位置。例如,如果要提取名为 的路径变量,可以使用:@PathVariable@RequestMappingcontentId

@RequestMapping(value = "/whatever/{contentId}", method = RequestMethod.POST)

编辑:此外,如果你的路径变量可以包含一个'.',并且你想要这部分数据,那么你需要告诉Spring获取所有内容,而不仅仅是'.'之前的东西:

@RequestMapping(value = "/whatever/{contentId:.*}", method = RequestMethod.POST)

这是因为Spring的默认行为是将URL的该部分视为文件扩展名,并将其从变量提取中排除。


答案 2

我因为一个愚蠢的错误而得到了这个错误,@PathVariable变量名称与@RequestMapping

例如

@RequestMapping(value = "/whatever/{**contentId**}", method = RequestMethod.POST)
public … method(@PathVariable Integer **contentID**){
}

它可能有助于他人


推荐