У меня есть контроллер 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 должен быть адресом электронной почты, но все, начиная с последней точки, обрезается. Поэтому вместо «[email protected]» я получаю «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 does not contain much:
swagger.include-pattern=/(?!api-docs|version).*