1

I have problem when trying to create get method which allows dots as a parameter.

    @GetMapping(path = "test/{id:.+}")
    @ResponseBody
    public String getTest(@PathVariable String id) {       

        return id;
    }

So it works for example for path

test/core.txt

But it does not for

test/core.something

I got error like

{
  "timestamp": 1623837131322,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.NoClassDefFoundError",
  "message": "Could not initialize class org.springframework.web.accept.PathExtensionContentNegotiationStrategy$ActivationMediaTypeFactory",
  "path": "/api/v1/test/core.somethign"
}

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.accept.PathExtensionContentNegotiationStrategy$ActivationMediaTypeFactory ...

Also tried to extend WebMvcConfigurationSupport with

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

I am using spring boot version 1.5.22.RELEASE. Do you have any idea what can be wrong, seems like my configuration is ignored somehow

package eu.test;

import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
@ComponentScan
@PropertySource(
        ignoreResourceNotFound = false,
        value = "classpath:application.properties")
public class ApplicationConfiguration extends WebMvcConfigurationSupport {
    @Override
    protected void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
}


wtomko
  • 35
  • 6
  • Does this answer your question? [Spring MVC @PathVariable with dot (.) is getting truncated](https://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated) – Pirate Jun 16 '21 at 10:05
  • Unfortunatelly not fully, I can use mapping like @GetMapping(path = "test/{id}/") but otherwise it won't work. I would like to avoid adding trailing slash. – wtomko Jun 16 '21 at 10:25

1 Answers1

1

Add below bean in your appconfig file.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport{

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
        handlerMapping.setUseSuffixPatternMatch(false);
        handlerMapping.setUseTrailingSlashMatch(false);
        return handlerMapping;
    }
}

Update : This class is deprecated. Now you have to use below code. I tested it and i'm posting here.

@Configuration
public class ApplicationConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
}

Output:

test/abc.tera : abc.tera

test/core.something : core.something

Pirate
  • 2,886
  • 4
  • 24
  • 42
  • I did it, it was executed but still I have same error "Could not initialize class org.springframework.web.accept.PathExtensionContentNegotiationStrategy$ActivationMediaTypeFactory", – wtomko Jun 16 '21 at 10:33
  • **UPDATE:** From my perspective it should work, but still I have same error – wtomko Jun 16 '21 at 11:25
  • Unfortunately I have to stay with this version of spring. – wtomko Jun 16 '21 at 11:33
  • ``` @Configuration @ComponentScan @PropertySource( ignoreResourceNotFound = false, value = "classpath:application.properties") public class ApplicationConfiguration extends WebMvcConfigurationSupport { @Override protected void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(false); } } ``` – wtomko Jun 16 '21 at 11:47
  • i wanted to see the imports. Please add the code in question. – Pirate Jun 16 '21 at 11:48
  • why did you imported RequestMappingHandlerMapping? – Pirate Jun 16 '21 at 12:01
  • I forgot to remove it – wtomko Jun 16 '21 at 12:49