2013-02-06 47 views
0

问题“”:弹簧安置@RequestMapping提取错误如果值包含

参见下面的Spring REST例如,如果如http://localhost:8080/site/google.com请求被提交,春回“谷歌“。看起来像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"; 

} 

} 

回答

1

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

{domain:.[\\S]*} 

你可能不得不摆弄正则表达式来让它正确。 ':'将路径变量名与正则表达式分开。

+0

这有史以来的工作? – CodeChimp

0

这是很简单的,只需加上“:+”与您的参数如下图所示:

这里的用户名是电子邮件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/[email protected]

0

只是让 '/' 和SiteController.java CHAGE注意到确保您的网址结束..

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