Spring Boot获取方法,按扩展名/ mime请求映射

时间:2016-07-30 18:31:28

标签: java spring http spring-mvc spring-boot

我正在使用Spring Boot来创建HTTP端点。我想有2个Get方法处理程序。一个用于http:// $ HOST / something / {key},另一个用于http:// $ HOST / something / {key} .xyz其中xyz是我编写的扩展名,而不是xml / json。 示例:http://localhost:8080/something/123应该转到method1,而http://localhost:8080/something/123.xyz应该转到method2。

这就是我的尝试:

@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);

}

这是我的终点

@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;
}

}

我的所有请求都是getAllExtensionsHandler,甚至当http :: / localhost:8080 / something / 123.xyz

我错过了什么? 我希望它成为正确的解决方案而不是破坏其他一切的hacky。 谢谢!

0 个答案:

没有答案