@Import(User.class)
@Configuration(proxyBeanMethods = true)
@EnableConfigurationProperties(Car.class)
//proxyBeanMEthods
// 设置为false意味着跳过扫描容器的步骤,直接new一个新的组件而不是使用容器中的组件
public class MyConfiguration implements WebMvcConfigurer {
@Bean
@ConditionalOnClass(name = "Pet")
public User getUser(){
return new User("yan",18,getPet());
}
//@Bean("tom")
public Pet getPet(){
return new Pet("tom");
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
//自定义转换器,转换自定义的字符串格式
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new Converter<String, Pet>() {
@Override
public Pet convert(String source) {
String[] split = source.split(",");
return new Pet(split[0]);
}
});
}
//如果想把自定义的类型也融入到参数内容协商里面去
//需要重写内容协商配置类
//注意:这里会覆盖所有的映射
//所以我们需要自己添加映射
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//configurer.strategies();
HashMap<String, MediaType> stringMediaTypeHashMap = new HashMap<>();
stringMediaTypeHashMap.put("xml",MediaType.APPLICATION_XML);
stringMediaTypeHashMap.put("json",MediaType.APPLICATION_JSON);
stringMediaTypeHashMap.put("yan",MediaType.parseMediaType("application/yan"));
//参数响应
ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = new ParameterContentNegotiationStrategy(stringMediaTypeHashMap);
//若想基于头的响应 则应该增加策略
HeaderContentNegotiationStrategy headStrategy=new HeaderContentNegotiationStrategy();
configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy,headStrategy));
}
//添加自定义messageConverter
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new YanMessageConverter());
}
}