Spring MVC缺少URI模板变量

时间:2013-11-06 02:43:33

标签: java spring spring-mvc model-view-controller

我有一个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)

3 个答案:

答案 0 :(得分:53)

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

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

如果你想要@PathVariable,你需要在@RequestMapping条目中指定占位符,告诉Spring路径变量在URI中的位置。例如,如果要提取名为contentId的路径变量,则应使用:

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

修改:此外,如果您的路径变量可能包含“。”而你想要那部分数据,那么你需要告诉Spring抓住所有东西,而不仅仅是'。'之前的东西:

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

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

答案 1 :(得分:14)

我因为一个愚蠢的错误而收到此错误,@PathVariable中的变量名称与@RequestMapping中的变量名称不匹配

例如

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

它可能会帮助其他人

答案 2 :(得分:0)

当您在 REST 定义中定义的映射变量与 @PathVariable 名称不匹配时,可能会发生此错误。

示例: 假设你在 REST 定义中定义了

@GetMapping(value = "/{appId}", produces = "application/json", consumes = "application/json")

那么在定义函数的时候,应该是

public ResponseEntity<List> getData(@PathVariable String appId)

当您使用除 REST 控制器定义中定义的 @PathVariable 之外的任何其他变量时,可能会发生此错误。 例如,下面的代码将引发错误,因为 ID 与 appId 变量名称不同:

public ResponseEntity<List> getData(@PathVariable String ID)