java 如何在处理 url 'dot' 字符时更改 Spring MVC 的行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4135329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:57:02  来源:igfitidea点击:

How to change Spring MVC's behavior in handling url 'dot' character

javaspring-mvc

提问by Alexey Buistov

I'm trying to migrate a web project off from Jersey to Spring MVC 3.0. The process was really straightforward up to the moment when I started to migrate the controllers supposed to handle URL's with dot notations: "/myApp/resources/create/root.subFolder1". Spring MVC seems to shamelessly cut the ".subFolder1"part from the URL, which happens deep inside framework code (see AbstractUrlHandlerMappingclass)

我正在尝试将一个 Web 项目从 Jersey 迁移到 Spring MVC 3.0。直到我开始迁移应该处理带有点符号的 URL 的控制器时,这个过程非常简单:“/myApp/resources/create/ root.subFolder1”。Spring MVC 似乎无耻地从 URL 中删除了“.subFolder1”部分,这发生在框架代码的深处(参见AbstractUrlHandlerMapping类)

uriTemplateVariables.putAll(getPathMatcher().extractUriTemplateVariables(matchingPattern, urlPath));

So my controller method gets invoked with rootpath parameter, not root.subFolder1

所以我的控制器方法被路径参数调用,而不是root.subFolder1

I'd really like to find a way to customize this behavior. Any advices?

我真的很想找到一种方法来自定义这种行为。有什么建议吗?

PS. The requirement is kinda to keep the existing URL structure, i.e. workarounds like switching to query params "/myApp/resources/create/?path=root.subFolder1" I cannot consider.

附注。要求有点保留现有的 URL 结构,即切换到查询参数“/myApp/resources/create/ ?path=root.subFolder1”之类的变通方法,我无法考虑。

PS. My Spring config looks like

附注。我的 Spring 配置看起来像

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

<context:component-scan base-package="my.app.pkg"/>

回答by AHungerArtist

Try this:

试试这个:

<bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="useDefaultSuffixPattern" value="false" />
    </bean>

This should make it so that Spring won't try to parse extensions.

这应该使 Spring 不会尝试解析扩展。

See also: Spring MVC @PathVariable getting truncated

另请参阅:Spring MVC @PathVariable 被截断

And: Spring Documentation

以及:Spring 文档

Potential config files:

潜在的配置文件:

web.xml (where I set the servlet stuff)

web.xml(我在其中设置 servlet 的东西)

<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/*.xml,classpath*:applicationContext.xml,classpath*:restApplicationContext.xml
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

And then in WEB/INF/spring/mvc-config.xml I have something like this:

然后在 WEB/INF/spring/mvc-config.xml 我有这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="useDefaultSuffixPattern" value="false" />
    </bean>
</beans>

回答by Josh

Another possibility which may be easier in some circumstances is to add a trailing /to the end of the URL.

在某些情况下可能更容易的另一种可能性是/在 URL 末尾添加尾随。

So your URL would be /myApp/resources/create/root.subFolder1/

所以你的网址是 /myApp/resources/create/root.subFolder1/

This works in my app using Spring MVC 3.1.

这适用于我使用 Spring MVC 3.1 的应用程序。

回答by Ryba

In order for

为了

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

to work you have to remove/comment out the

要工作,您必须删除/注释掉

<mvc:annotation-driven />
<mvc:default-servlet-handler />

and any other mvc: configurations defined as they override your custom bean declarations.

和任何其他 mvc: 配置定义为它们覆盖您的自定义 bean 声明。

回答by Robert Beltran

This was a bitch. A guy on our team here got it to work over the weekend. We were going to go with underscore but he got it. He removed @Controller from the controller class attributes and left @RequestMapping blank for the class. On the public methods for Http get and put he added "/2.0/" to the @RequestMapping. Example below.

这是个婊子。我们团队的一个人在周末让它工作。我们打算用下划线,但他明白了。他从控制器类属性中删除了@Controller,并将类的@RequestMapping 留空。在 Http get 和 put 的公共方法上,他向@RequestMapping 添加了“/2.0/”。下面举例。

@RequestMapping
public class EndpointController {

@RequestMapping(value="/2.0/endpoint", method = RequestMethod.POST,
consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EndpointView> create( @Valid @RequestBody EndpointView endpointParams, HttpServletRequest request )

Good luck everyone. This problem sucked.

祝大家好运。这个问题糟透了。

回答by Michal Korecki

I did almost the same as Robert Beltran but I left @Controller annotation for the class. Important is to put "/1.0/" to the @RequestMapping on the methods, not in class annotation.
My example:

我和 Robert Beltran 的做法几乎一样,但我为班级留下了 @Controller 注释。重要的是将“/1.0/”放在方法上的@RequestMapping 中,而不是放在类注释中。
我的例子:

@Controller
@RequestMapping("")
public class ClientApi {

private final String API_PREFIX = "/api/1.0/client";

@RequestMapping(value = API_PREFIX + "/{clientId}", method = RequestMethod.GET)
@ResponseBody
public ClientDetailsImpl get(@PathVariable String clientId) {
    // my code
}
}

回答by Michael

The above answer should correct, but be sure to remove <mvc:annotation-driven/>from your XML configuration, otherwise the bean definition is overruled by the one part of the annotation-driven tag.

上面的答案应该是正确的,但一定<mvc:annotation-driven/>要从你的 XML 配置中删除,否则 bean 定义会被注释驱动标签的一部分所否决。

See the java docsand the source codefor all the configuration you miss when this convenience tag is removed.

请参阅Java 文档源代码,了解删除此便利标记后您遗漏的所有配置。