2

I have a Spring boot (v2.3.5.RELEASE) REST API and cannot seem to fix an encoding problem with path variables. I have tried every solution that found online but nothing helped. Here is the Main class:

@SpringBootApplication
public class RestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestApplication.class, args);
    }
}

The RestController (the method is never executed when sending the request to that URL):

@RestController
@RequestMapping("/mvn/packages")
public class ModuleApi {

    @Autowired
    ModuleApiService service;

    @GetMapping(value = "/{pkg}/{pkg_ver}/modules/{namespace}/metadata", produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<String> getModuleMetadata(@PathVariable("pkg") String package_name,
                                             @PathVariable("pkg_ver") String package_version,
                                             @PathVariable("namespace") String module_namespace) {
        return service.getModuleMetadata(package_name, package_version, module_namespace);
    }
}

The Configuration class:

@Configuration
public class CorsConfiguration
{
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding("UTF-8");
        registrationBean.setFilter(characterEncodingFilter);
        return registrationBean;
    }
}

The application.properties:

logging.level.org.springframework.web=DEBUG
server.port=8080
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

I even added encoding to the spring-boot-maven-plugin in the pom.xml:

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.6.RELEASE</version>
                <configuration>
                    <mainClass>RestApplication</mainClass>
                    <jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
                </configuration>
            </plugin>

But the result is still the same. When sending a request to http://localhost:8080/mvn/packages/junit:junit/4.12/%2Fjunit.framework%2FAssert i.e. mvn/packages/{pkg}/{pkg_ver}/modules/{namespace}/metadata and namespace is encoded, it returns HTTP 400 - BAD REQUEST.

However, when I try http://localhost:8080/mvn/packages/junit:junit/4.12/foo (does not need encoding/decoding), it works.

I also tried using ALLOW_ENCODED_SLASH property. Main class:

@SpringBootApplication
public class RestApplication {

    public static void main(String[] args) {
        System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
        SpringApplication.run(RestApplication.class, args);
    }
}

But in this case it cannot resolve the request to that mapping and returns 404 - NOT FOUND:

2020-12-10 17:03:27.044 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/mvn/packages/junit:junit/4.12/%2Fjunit.framework%2FAssert", parameters={}
2020-12-10 17:03:27.053 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-12-10 17:03:27.056 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2020-12-10 17:03:27.056 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2020-12-10 17:03:27.060 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2020-12-10 17:03:27.062 DEBUG 105120 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2020-12-10 17:03:27.083 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2020-12-10 17:03:27.087 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Can anyone help me please?

  • Possible duplicate: https://stackoverflow.com/questions/13482020/encoded-slash-2f-with-spring-requestmapping-path-param-gives-http-400 – waxwing Dec 10 '20 at 13:00
  • Yes, I already tried the solution from that post. It also suggests a workaround, but I would prefer a solution instead of a workaround. If there is indeed no solution, then will have to go for a workaround. – Mihhail Sokolov Dec 10 '20 at 14:18
  • Take a look at this one as well: https://stackoverflow.com/questions/51108291/springboot-doesnt-let-through-percentage-and-backslash-in-url Consider using wildcard matchers as described on the first answer – Piaget Hadzizi Dec 10 '20 at 14:20
  • Thank you, I saw other people using wildcards and matching URLs themselves, however, I have quite a lot of such URLs, and using wildcard and then matching all of them is not really a feasible solution. – Mihhail Sokolov Dec 10 '20 at 14:33

0 Answers0