【问题标题】:Spring mvc @PathVariable gives 'The request sent by the client was syntactically incorrect.'Spring mvc @PathVariable 给出“客户端发送的请求在语法上不正确。”
【发布时间】:2018-01-08 14:25:15
【问题描述】:

您好,我是 Spring MVC 的新手,正在尝试一些简单的示例。

我在 Eclipse 中使用 archtype 'spring-mvc-archtype' 创建了一个新的 Spring mvc maven 项目。

我正在尝试点击 url 并在 Map 中使用 @PathParam 访问 PathVariables

这是我的控制器类

@Controller
@RequestMapping(value="/greet")
public class HomeController {

@RequestMapping(value="/welcome/{countryName}/{userName}")
public ModelAndView test(@PathVariable("userName") String userName, @PathVariable("countryName") String countryName) throws IOException{
    ModelAndView mav = new ModelAndView("home");
    mav.addObject("mymessage", "Welcome To "+countryName+" , Spring MVC - "+userName);
    return mav;
}

@RequestMapping(value="/hi/{cn}/{un}")
public ModelAndView hi(@PathVariable Map<String, String> pathVars){

    String countryName = pathVars.get("cn");
    String userName = pathVars.get("un");

    ModelAndView mav = new ModelAndView("home");
    mav.addObject("mymessage", "Hi "+userName+" in "+ countryName);
    return mav;
}
}

这是配置类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.gyanbang.kiran.SpringMvc")
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}   
}

所以当我运行它并点击 http://localhost:8080/SpringMvc/greet/welcome/India/user1 效果很好,我得到了想要的结果。

但在 http://localhost:8080/SpringMvc/greet/hi/India/user1 上,它会给出 400 错误消息 '客户端发送的请求语法错误。'

我尝试了几个选项,还尝试将 spring-webmvc 版本更改为 4.1.6.RELEASE 但在这种情况下它给了我 404。

任何帮助都会很棒。 提前致谢。

【问题讨论】:

  • 您是否有特定原因希望将变量作为地图?
  • @geoffreydv 没有具体原因,但我想尝试一下,看看它是如何工作的。除此之外,我发现它作为一个概念和实现路径变量的方式很容易。谢谢
  • 已解决:嗯,看来我需要将 spring-context 和 spring-test 工件版本更新为 4.3.9.RELEASE 和 spring-webmvc 为 4.1.6.RELEASE。更新版本后,上面编写的代码可以正常工作。谢谢。 :-)

标签: java spring maven spring-mvc path-variables


【解决方案1】:

@PathVariable可以使用Map&lt;String,String&gt;,你需要使用&lt;mvc:annotation-driven/&gt;变成ApplicationContaxt.xml。您可以在下面找到代码:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven/>

【讨论】:

    【解决方案2】:

    从一开始我就很想知道为什么您的解决方案不起作用。

    我检查了收集到地图中的路径变量 - 它们工作正常。我认为您的问题与调度程序 servlet 有关。您可以尝试配置一个或搜索一些现有的解决方案,我建议使用 spring box 的解决方案。

    请在 git 上查看我的解决方案。关注build.gradle文件

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
    

    ...

    dependencies {
        compile("org.springframework.boot:spring-boot-starter-thymeleaf")
        runtime('org.springframework.boot:spring-boot-devtools')
    }
    

    Link to my git project solution

    您的代码行数很少,因此您应该清楚。但如果有不清楚的地方 - 不要犹豫,并在评论中提问。

    【讨论】:

      【解决方案3】:

      根据 Spring 文档:

      @PathVariable 参数可以是任何简单类型,例如 int、long、Date 等。

      (虽然这种行为可以是customised)。

      此外,您的控制器指定一个带有两个参数的 URI:

      @RequestMapping(value="/hi/{cn}/{un}")
      

      但是控制器方法只声明了一个:@PathVariable Map&lt;String, String&gt; pathVars

      如果您的目标是(正如这表明:“尝试一些简单的示例”)使用 Spring MVC 证明一些从前到后的调用,那么也许您应该首先保持简单,例如:

      @RequestMapping(value="/hi/{cn}/{un}")
      public ModelAndView hi(@PathVariable String cn, @PathVariable String un){
          ...
      }
      

      【讨论】:

      • 感谢您的宝贵回复。但是我已经在控制器中定义了@RequestMapping(value="/welcome/{countryName}/{userName}") public ModelAndView test(@PathVariable("userName") String userName, @PathVariable("countryName") String countryName) throws IOException{ 但是我想试试这个具体的情况。
      • 好的,如果您想尝试使用 PathVariable 传递地图,那么您需要阅读 Spring 如何支持它。我在上面的答案中发布了一个链接。但是,无论您是否传递 Map,如果您的控制器方法接受一个 PathVariable 并且您的 RequestMapping 声明了两个参数占位符,那么您将得到一个异常。您在问题中发布的异常的根本原因是:(a)使用 Map 而不告诉 Spring 如何处理 Map 和(b)定义两个参数占位符但只定义一个 PathVariable。
      猜你喜欢
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2015-10-31
      相关资源
      最近更新 更多