获取请求仅适用于尾部斜杠(弹簧 REST 注释) [英] Get request only works with trailing slash (spring REST annotations)

查看:19
本文介绍了获取请求仅适用于尾部斜杠(弹簧 REST 注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring REST 控制器:

I have a Spring REST Controller:

@RestController
@RequestMapping(value = "/myresource")
public class MyResourceController {
  ...
}

使用 GET 请求方法:

With a GET request method:

@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3}", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")    
public ResponseEntity<MyResponseType> getMyResource(
  @ApiParam(value = "...",...)
  @PathVariable("value1") String value1,
  @ApiParam(value = "...",...)
  @PathVariable("value2") String value2,
  @ApiParam(value = "...",...)
  @PathVariable("value3") String value3) {
//...
}

我希望可以通过以下方式调用此方法:

I would expect this method to be callable with:

http://myserver:8080/myresource/value1/value2/value3

但它只能通过斜杠到达:

But it is only reachable with a trailing slash:

http://myserver:8080/myresource/value1/value2/value3/

为什么会这样或导致这种情况的原因是什么?

Why is that so or what is causing this?

Swagger 假设尾部没有斜杠,我现在无法使用 swagger 发送请求.

Swagger assumes there are no trailing slashes and I can't send a request with swagger now.

我该怎么做才能使第一个 URL 有效而第二个无效?

What can I do to only make the first URL work but not the second?

非常感谢您的评论和回答.

Thanks a lot in advance for your comments and answers.

编辑 2:

我发现如果没有斜线,value3 的信息是不完整的.value3 必须是电子邮件地址,但从最后一个点开始的所有内容都被切断了.因此,我收到的不是myemail@something.de",而是myemail@something".

I found out that without the slash the information of value3 is incomplete. value3 must be an email address but everything from the final dot is cut off. So instead of "myemail@something.de" I am getting "myemail@something".

这解释了为什么我无法获得正确的结果(无内容和 HTTP 状态代码 404).但我仍然不明白为什么会发生这种情况.此外,如果我的电子邮件地址在顶级域(如 .com)中包含三个字母,则请求永远不会到达 GET 方法getMyResource"......

This explains why I can't get a proper result (no content and HTTP status code 404). But I still don't understand why this happens. Further than that, if I have an email address with three letters in the top level domain (like .com) the request never reaches the GET method "getMyResource" ....

我的 web.xml 看起来像这样:

Edited 1: My web.xml looks like that:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>(my) Services</display-name>
<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Provide ServletContext -->
<listener>
    <listener-class>my.ServletContextUtils</listener-class>
</listener>

<!-- Expose request to current thread (required for session and request-scoping) -->
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/config/spring-mvc-servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

我的 spring-mvc-servlet.xml 看起来像这样:

And my spring-mvc-servlet.xml looks like that:

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:util="http://www.springframework.org/schema/util"
xmlns:env="http://my/schema"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-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
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-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/util
    http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://my/schema
    http://my/schema/env-0.3.xsd">

<env:initialization />
<context:annotation-config />
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:component-scan base-package="my.myresource.restapi" />

<context:property-placeholder
    ignore-unresolvable="true"
    location="
        WEB-INF/config/${my.environment}/webapp.properties" />

<!-- Swagger -->

<!-- swagger config -->
<bean class="my.restapi.swagger.SwaggerConfig"/>

webapp.properties 包含的内容不多:

webapp.properties does not contain much:

swagger.include-pattern=/(?!api-docs|version).*

推荐答案

我在这里找到了答案:

Spring MVC @PathVariable 被截断

这似乎只发生在最后一个参数上,解决方案是对最后一个值使用正则表达式:

This seems to happen only with the last parameter and the solution is to use a regex for the last value:

@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}"

这对我有用.

这篇关于获取请求仅适用于尾部斜杠(弹簧 REST 注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆