java – 获取请求仅适用于尾部斜杠(Spring REST注释)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 获取请求仅适用于尾部斜杠(Spring REST注释),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4911字,纯文字阅读大概需要8分钟。
内容图文
![java – 获取请求仅适用于尾部斜杠(Spring REST注释)](/upload/InfoBanner/zyjiaocheng/805/730fcabffa794f5f9f5b334f72b6195c.jpg)
我有一个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).*
解决方法:
我有很多使用Spring Rest注释的应用程序,我的API可以使用或不使用尾部斜杠.
它可能是您的Spring MVC配置或您正在使用的http服务器的问题.
你可以用Spring MVC配置发布一些代码片段吗?很高兴知道您正在使用哪个Spring版本以及如何为webapp提供服务.
编辑1:
当我在你的第二次编辑中读到你的电子邮件参数有问题吗?
您可以尝试使用类似的东西,让我知道它是否解决了这个问题?
@RequestMapping(method = GET,value =“/ {value1} / {value2} / {value3:.}”……
问题似乎与网址中的点有关.
参考:Spring MVC @PathVariable with dot (.) is getting truncated
内容总结
以上是互联网集市为您收集整理的java – 获取请求仅适用于尾部斜杠(Spring REST注释)全部内容,希望文章能够帮你解决java – 获取请求仅适用于尾部斜杠(Spring REST注释)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。