【问题标题】:Spring MVC @PathVariable with url address throwing error带有url地址抛出错误的Spring MVC @PathVariable
【发布时间】:2016-05-09 13:32:15
【问题描述】:

有没有办法在 @PathVariable 中发送像“https://jira.springsource.org/browse/SPR-6164”这样的参数作为 URI 路径的一部分...在 Spring Web MVC 中

@RequestMapping(value = "/Details/{Emailaddress}/{URL}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Status Details(@PathVariable("Emailaddress") String Emailaddress,
             @PathVariable("URL") String URL) {
try {
            bankingData.callingmethod(Emailaddress,URL);
            return new Status(1, "Details added !");
        } catch (Exception e) {
            // e.printStackTrace();
            return new Status(0, e.toString());
        }
    }

错误:请求的资源不可用

【问题讨论】:

  • 是否使用 {URL} 来标识您要使用的资源?如果不是,{URL} 应该被接受为查询参数,而不是路径变量。无论如何,您仍然可以通过对 URL 部分进行编码来发送 http URL 作为查询参数或路径变量。
  • 尝试发布完整方法
  • @Fahim Farook 对 URL 部分进行编码应该只能在客户端完成,还是我也可以在服务器端完成。?
  • @Abdelhak 请检查修改后的发布代码
  • 你能把你使用的网址贴出来吗?

标签: java spring spring-mvc


【解决方案1】:

是的,但您必须正确编码。使用URLEncoder.encode() 这样做:

String url = 
    "http://server/Details/"+URLEncoder.encode("http://myserver/mypath/to/url/", "UTF-8");

此代码在构建 URL 时必须在客户端执行。编码必须同时应用于 pat 元素和查询参数。

【讨论】:

  • 你能解释一下,我应该在哪里编码或将上面的代码放在我的上下文中?
【解决方案2】:

尝试在你的方法中像{URL:.+}这样写url

   @RequestMapping(value = "/Details/{Emailaddress}/{URL:.+}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Status Details(@PathVariable("Emailaddress") String Emailaddress,
         @PathVariable("URL") String URL) {

带点.的requestParameter被截断。

【讨论】:

猜你喜欢
  • 2017-06-18
  • 2015-04-30
  • 1970-01-01
  • 2014-09-16
  • 2013-04-26
  • 2013-11-17
  • 2018-03-18
相关资源
最近更新 更多