Get request only works with trailing slash (spring REST annotations)

I have a Spring REST controller:

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

      

With the request method 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) {
//...
}

      

I would expect this method to be called with:

http://myserver:8080/myresource/value1/value2/value3

      

But it is only available with a trailing slash:

http://myserver:8080/myresource/value1/value2/value3/

      

Why is this so or what is causing it?

Swagger assumes there are no forward slashes and now I cannot submit a swagger request.

What can I do to make only the first url, but not the second one?

Thanks a lot for your comments and responses.


Edited 2:

I found out that without the slash, the value3 information is incomplete. Value3 should be an email address, but everything from the endpoint is disabled. So instead of " myemail@something.de " I get "myemail @something".

This explains why I cannot get the correct result (no content and HTTP 404 status code). But I still don't understand why this is happening. Moreover, if I have a 3-letter email address in a top-level domain (eg .com), the request never reaches the "getMyResource" GET method ....


Edited 1: My web.xml looks like this:

<?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 this:

<?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 doesn't contain much:

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

      

+3


source to share


3 answers


I found the answer here:

Spring MVC @PathVariable being trimmed

This seems to only happen with the last parameter, and the solution is to use a regex for the last value:



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

      

This works for me.

+2


source


I have many applications that use Spring Rest annotations and my APIs work with or without forward slashes.

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

Could you please post some code snippets with Spring MVC config. It would be nice to know which version of Spring you are using and how you serve the webapp.

Edit 1:

As I read in the second edit, are you having problems with your email options?



Can you try something like this and let me know if it solves the problem?

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

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

Ref: Spring MVC @PathVariable with dot (.) Is truncated

+2


source


in web.xml file

<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>

      

what if you try without * just / (as shown in the picture), what result do you have?

<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>

      

+1


source







All Articles