Get请求仅适用于斜线(春季REST注释) [英] Get request only works with trailing slash (spring REST annotations)

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

问题描述

我有一个春天的REST控制器:

  @RestController
@RequestMapping(值=/ myresource)
公共类MyResourceController {
  ...
}

通过一个GET请求方式:

  @RequestMapping(方法= GET,值=/ {值1} / {值2} / {} VALUE3,产生= MediaType.APPLICATION_JSON_VALUE +;字符集= UTF-8 )
公共ResponseEntity< MyResponseType> getMyResource(
  @ApiParam(值=...,...)
  @PathVariable(值1)字符串值1,
  @ApiParam(值=...,...)
  @PathVariable(值2)字符串值2,
  @ApiParam(值=...,...)
  @PathVariable(VALUE3)字符串值3){
// ...
}

我希望这种方法可以被调用于

  HTTP:// MYSERVER:8080 / myresource /值1 /值2 / VALUE3

但它只是到达一个斜线:

  HTTP:// MYSERVER:8080 / myresource /值1 /值2 / VALUE3 /

为什么会这样或者是什么原因造成的?

扬鞭假定没有尾随斜线,现在我不能发送与招摇的请求。

我能做些什么来只能使第一个URL工作,但不是第二个?

感谢很多提前为您的意见和解答。


编辑2:

我发现,如果没有斜线VALUE3的信息是不完整的。值3必须从最终点的电子邮件地址,但一切被切断。
因此,而不是myemail@something.de我得到myemail @的东西。

这解释了为什么我不能得到正确的结果(没有内容和HTTP状态code 404)。但我还是不明白为什么会这样。而且重要的是,如果我有在顶级域名三个字母(例如.com)的电子邮件地址的请求不会到达GET方法getMyResource......


编辑1:
我的web.xml看起来像:

 <?XML版本=1.0编码=UTF-8&GT?;< web应用程序的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
的xmlns =htt​​p://java.sun.com/xml/ns/j2ee
XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
版本=2.4>
<显示-名称>(我的)服务< /显示-名称>
&所述;滤光器>
    <过滤器名称>&SetCharacterEncodingFilter LT; /过滤器名称>
    <过滤器类和GT; org.springframework.web.filter.CharacterEncodingFilter< /过滤器类>
    <初始化参数>
        <参数-名称>&编码LT; /参数 - 名称>
        <参数值> UTF-8< /参数值>
    < /初始化参数>
    <初始化参数>
        <参数-名称>&forceEncoding LT; /参数 - 名称>
        <参数值>真< /参数值>
    < /初始化参数>
< /滤光器>
<过滤器映射>
    <过滤器名称>&SetCharacterEncodingFilter LT; /过滤器名称>
    < URL模式> / *< / URL模式>
< /过滤器映射><! - 提供的ServletContext - >
<听者GT;
    <监听级> my.ServletContextUtils< /监听级>
< /听者GT;<! - 揭露请求当前线程(会话和请求作用域需要) - GT;
<听者GT;
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
&LT; /听者GT;&LT;&servlet的GT;
    &LT; servlet的名称&gt;弹簧MVC&LT; / servlet的名称&gt;
    &LT;的servlet类&GT; org.springframework.web.servlet.DispatcherServlet&LT; / servlet的类&GT;
    &LT;初始化参数&GT;
        &LT;参数-名称&gt;&contextConfigLocation的LT; /参数 - 名称&gt;
        &LT;参数值&GT;
            WEB-INF /配置/弹簧MVC-servlet.xml中
        &LT; /参数值&GT;
    &LT; /初始化参数&GT;
    &所述;负载上启动→1&下; /负载上启动&GT;
&LT; / servlet的&GT;
&LT; Servlet映射&GT;
    &LT; servlet的名称&gt;弹簧MVC&LT; / servlet的名称&gt;
    &LT; URL模式&GT; / *&LT; / URL模式&GT;
&LT; / Servlet映射&GT;&LT; / web-app的&GT;

和我的春天MVC-servlet.xml中看起来像:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;豆的xmlns =htt​​p://www.springframework.org/schema/beans
XMLNS:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:上下文=htt​​p://www.springframework.org/schema/context
的xmlns:MVC =htt​​p://www.springframework.org/schema/mvc的xmlns:AOP =htt​​p://www.springframework.org/schema/aop
的xmlns:TX =htt​​p://www.springframework.org/schema/tx的xmlns:UTIL =htt​​p://www.springframework.org/schema/util
的xmlns:ENV =HTTP://我的/模式
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://我的/模式
    HTTP://my/schema/env-0.3.xsd&GT;&LT; ENV:初始化/&GT;
&LT;背景:注解配置/&GT;
&LT; MVC:注解驱动/&GT;
&LT; MVC:默认的servlet处理程序/&GT;
&LT;背景:组件扫描基包=my.myresource.restapi/&GT;&LT;背景:物业占位符
    忽略-无法解决的=真
    位置=
        WEB-INF /配置/ $ {} my.environment /webapp.properties/&GT;&LT;! - 扬鞭 - &GT;&LT;! - 招摇配置 - &GT;
&LT; bean类=my.restapi.swagger.SwaggerConfig/&GT;

webapp.properties不包含多少:

  swagger.include图案= /(?! API-文档|版本)*。


解决方案

我有很多使用Spring休息注释的应用程序和API的我的作品有或没有尾随斜线。

这可能是您的Spring MVC的配置或HTTP服务器使用的是一个问题。

您可以张贴一些code片段与大家Spring MVC的配置。这将是很好的知道你正在使用的Spring版本以及你是如何服务于Web应用程序。

编辑1:

我在您有电子邮件参数问题你第二个编辑没有看错?

您可以尝试使用类似的东西,让它是否解决了问题我知道的?

@RequestMapping(方法= GET,值=/ {值1} / {值2} / {值3:+}...

这个问题似乎是在url点。

参考:<一href=\"http://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated\">Spring MVC @PathVariable用点(。)是越来越截断

I have a Spring REST Controller:

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

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 assumes there are no trailing slashes and I can't send a request with swagger now.

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.


Edited 2:

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".

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" ....


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>

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 does not contain much:

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

解决方案

I have a lot of applications using Spring Rest annotations and my APIs works with or without the trailing slashes.

It may be a problem with your Spring MVC configuration or with the http server you are using.

Could you post some code snippets with you Spring MVC configurations. It would be good to known which Spring version you are using and how are you serving the webapp.

Edit 1:

As I read in your second edit you are having problems with email parameters right?

Can you try using something like that and let me known if it resolves the issue?

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

The problem seems to be with the dot in the url.

Reference: Spring MVC @PathVariable with dot (.) is getting truncated

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

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