Java Get请求仅适用于尾部斜杠(spring REST注释)
我有一个弹簧休息控制器:Java Get请求仅适用于尾部斜杠(spring REST注释),java,spring,rest,annotations,trailing-slash,Java,Spring,Rest,Annotations,Trailing Slash,我有一个弹簧休息控制器: @RestController @RequestMapping(value = "/myresource") public class MyResourceController { ... } 使用GET请求方法: @RequestMapping(method = GET, value = "/{value1}/{value2}/{value3}", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=U
@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/
为什么会这样,或者是什么导致了这种情况
斯威格假设没有后续斜杠,我现在无法使用斯威格发送请求
我该怎么做才能只让第一个URL工作,而不让第二个URL工作
非常感谢您的评论和回答
编辑2: 我发现没有斜杠,value3的信息是不完整的。值3必须是电子邮件地址,但从最后一个点开始的所有内容都被切断。 因此,与其说myemail@something.de“我要走了”myemail@something“ 这解释了为什么我不能得到一个正确的结果(没有内容和HTTP状态代码404)。但我还是不明白为什么会这样。此外,如果我在顶级域(比如.com)中有一个包含三个字母的电子邮件地址,那么请求永远不会到达GET方法“getMyResource”
编辑1: My 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>
<?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"/>
我有很多应用程序使用SpringREST注释,我的API使用或不使用斜杠 这可能是您的SpringMVC配置或您正在使用的http服务器的问题 你能在Spring MVC配置中发布一些代码片段吗。最好知道您使用的是哪个Spring版本,以及如何为webapp提供服务 编辑1: 正如我在你的第二次编辑中读到的,你在电子邮件参数方面有问题,对吗 你能试着用类似的东西,如果它解决了问题,请告诉我吗
@RequestMapping(method=GET,value=“/{value1}/{value2}/{value3:.+}”…
问题似乎在于url中的点
参考:在web.xml文件中
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>
春季mvc
/*
如果您尝试不使用*just/(如图所示),您会得到什么结果
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
春季mvc
/
我在这里找到了答案:
似乎只有最后一个参数才会出现这种情况,解决方案是对最后一个值使用正则表达式:
@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}"
这对我来说很有用。谢谢你的问题。我刚刚添加了我的配置。但是,我对POST请求没有问题。斯威格正确地发布了请求并收到了回复。哦,是的,没有看到你的编辑,是的,我现在自己也发现了。确实有效!@pathvariable和电子邮件地址类似于已知问题。你可以有一个l看这里,还有这里
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}"