我有一个 Spring REST 控制器:
@RestController
@RequestMapping(value = "/myresource")
public class MyResourceController {
...
}
使用 GET 请求方法:
@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) {
//...
}
我希望此方法可以通过以下方式调用:
http://myserver:8080/myresource/value1/value2/value3
但它只能通过斜杠到达:
http://myserver:8080/myresource/value1/value2/value3/
为什么会这样或者是什么原因造成的?
Swagger 假设没有尾部斜杠,我现在不能用 swagger 发送请求。
我该怎么做才能使第一个 URL 工作而不是第二个?
非常感谢您的评论和回答。
编辑2:
我发现没有斜线 value3 的信息是不完整的。value3 必须是一个电子邮件地址,但从最后一个点开始的所有内容都会被截断。因此,我得到的不是“myemail@something.de”,而是“myemail@something”。
这解释了为什么我不能得到正确的结果(没有内容和 HTTP 状态代码 404)。但我仍然不明白为什么会这样。此外,如果我在顶级域(如 .com)中有一个包含三个字母的电子邮件地址,则请求永远不会到达 GET 方法“getMyResource”......
编辑 1:我的 web.xml 看起来像这样:
<?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 看起来像这样:
<?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 不包含太多内容:
swagger.include-pattern=/(?!api-docs|version).*