0

I'm using Spring Boot to create an HTTP endpoint. I would like to have 2 Get method handlers. One for http://$HOST/something/{key} and a separate one for http://$HOST/something/{key}.xyz Where xyz is an extension I made up, and it's not xml/json. Example: http://localhost:8080/something/123 should go to method1, and http://localhost:8080/something/123.xyz should go to method2.

This is what I tried:

@Configuration
@Import({
    DispatcherServletAutoConfiguration.class,
    HttpMessageConvertersAutoConfiguration.class,
    ServerPropertiesAutoConfiguration.class
})
public class SpringConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter{
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "invalid")
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter()
{
    return null;
}

@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "invalid")
public OrderedHttpPutFormContentFilter httpPutFormContentFilter()
{
    return null;
}

@Bean
@Override
@ConditionalOnProperty(prefix = "spring.mvc", name = "invalid")
public RequestContextFilter requestContextFilter()
{
    return null;
}

@Primary
@Bean(name = "jacksonObjectMapper")
public ObjectMapper jacksonObjectMapper()
{
    return new Jackson2ObjectMapperBuilder()
        .propertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
        .serializationInclusion(JsonInclude.Include.NON_NULL)
        .build();
}

@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters)
{
    converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapper()));


    ArrayList<MediaType> list = new ArrayList<>();
    MediaType mediaType = new MediaType("application","xyz");
    list.add(mediaType);
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    stringHttpMessageConverter.setSupportedMediaTypes(list);
    List<MediaType> supportedList = stringHttpMessageConverter.getSupportedMediaTypes();
    converters.add(stringHttpMessageConverter);

}

And here is my endpoint

@CrossOrigin
@RestController
@RequestMapping(value = "/something")
public class MyEndpoint {

@ResponseBody
@RequestMapping(value = "/{key}",method = RequestMethod.GET,consumes = "application/xyz")
public String getXyzHandler(@PathVariable("key") final String  key, final HttpServletRequest httpRequest)
{
    return null;
}

@ResponseBody
@RequestMapping(value = "/{key}",method = RequestMethod.GET,consumes = "!application/xyz")
public String getAllExtensionsHandler(@PathVariable("key") final String  key)
{

    return null;
}

}

All my requests are going to getAllExtensionsHandler and even when http::/localhost:8080/something/123.xyz

What am I missing? I'm want it to be the right solution and not something hacky that would break everything else. Thank you!

Michael P
  • 2,017
  • 3
  • 25
  • 33
  • Try a path variable with regex. Also see this http://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated – Evgeni Dimitrov Jul 30 '16 at 20:27
  • 1
    I think this is a bad practice. Why don't you introduce a custom content type? like application/json do something like application/xyz. This is what this header should used for. – Yannic Bürgmann Jul 31 '16 at 10:41
  • That would be great, and I would love to do it. Would you be able to provide me with some tips/examples on how to do it? Notice that the Get method is set for me and I can't change it. – Michael P Aug 01 '16 at 04:52

0 Answers0