【问题标题】:Spring REST @RequestMapping Extract Incorrectly If Value Contains ‘.’如果值包含“。”,则 Spring REST @RequestMapping 提取不正确
【发布时间】:2013-02-06 17:32:38
【问题描述】:

问题:

参见下面的 Spring REST 示例,如果提交了 http://localhost:8080/site/google.com 之类的请求,则 Spring 返回“google”。看起来像春天对待“。”作为文件扩展名,并提取一半的参数值。

Spring 必须返回“google.com”。怎么办?

SiteController.java

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/site")
public class SiteController {

@RequestMapping(value = "/{domain}", method = RequestMethod.GET)
public String printWelcome(@PathVariable("domain") String domain,
    ModelMap model) {

    model.addAttribute("domain", domain);
    return "domain";

}

}

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    我相信您可以在@ResourceMapping 的路径变量中使用正则表达式。例如,你可以这样做

    {domain:.[\\S]*}
    

    您可能不得不摆弄一些正则表达式才能使其正确。 ':' 将路径变量名称与正则表达式分开。

    【讨论】:

      【解决方案2】:

      这很简单,只需在您的参数中添加“:.+”,如下所示:

      这里的用户名是电子邮件ID

      @RequestMapping(value = "checkuser/{username:.+}" , method=RequestMethod.GET)
      @ResponseBody
      boolean getUserName(@PathVariable("username") String userName){ 
          boolean isUserAvailable = userDao.checkAvailable(userName); 
          return isUserAvailable; 
      }
      

      我正在从 URL 传递值,例如: baseurl/checkuser/test@test.com

      【讨论】:

        【解决方案3】:

        只要确保你的 url 以 '/' 结尾并且 SiteController.java chage 注意..

        change http://xxx/site/google.com to http://xxx/site/google.com/

        【讨论】:

          猜你喜欢
          • 2018-11-03
          • 2019-01-30
          • 2018-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多