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!