SpringMVCと@Configuration:conversionerviceとhttpmessageConverterを登録します
-
22-12-2019 - |
質問
XMLベースの設定を取り除くために、春と@ configurationスタイルが本当に好きです。私はサービスとリポジトリレイヤーに正常に使用されます。私も愛が愛しているのは、依存噴射機能とJDO / JPA / JDBCユーティリティです。
私が本当に得られないものは、Spring WebMVCがどのように機能するかです。私にとっては多すぎる魔法が多すぎる。 (そして@EnableAutoc onfigurationとそれ以上魔法が紹介されています。簡単なプロトタイピングのために良い、維持が難しい)。
それが私のwebアプリケーションを設定する方法:
public class SpringWebBooter implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
rootContext.register(SpringConfiguration.class); //main configuration class for all beans
rootContext.refresh();
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setParent(rootContext);
ctx.register(SpringWebConfiguration.class); //web context configuration class
ServletRegistration.Dynamic api = servletContext.addServlet("api", new DispatcherServlet(ctx));
api.setLoadOnStartup(1);
api.addMapping("/api/*");
}
}
.
Type ConvertersとHttpMessageConvertersを追加したいので、SpringWebConfigurationクラスで試してみました:
@EnableWebMvc
@Configuration
@ComponentScan
public class SpringWebConfiguration {
//works but feels very *magic*
@Autowired
public void configureConversionService(FormattingConversionService conversionService) {
conversionService.addConverter(new PointConverter(GEOMETRY_FACTORY));
conversionService.addConverterFactory(new StringToEnumConverterFactory());
}
//not working yet
@Bean
public MappingJackson2HttpMessageConverter createJsonMessageConverter() {
ObjectMapper o = new ObjectMapper();
o.enable(SerializationFeature.INDENT_OUTPUT);
MappingJackson2HttpMessageConverter c = new MappingJackson2HttpMessageConverter();
c.setObjectMapper(o);
return c;
}
}
.
Dispatcherサーブレットを構築するときに直感的なことを直感的に見つけることは、タイプコンバータとメッセージコンバータを追加することです。それはいくつかの疑わしい自動化または豆の作成よりはるかに明確になるでしょう。ディスパッチャーサーブレットが私の豆を内部に撮影したことを常に「希望」しますが、それはしばしば試行錯誤です。 監督方法で春のMVCを設置することは可能ですか?より少ない魔法とより具体的なインスタンス化と#addhttpmessageConverter(...)呼び出し?
基本的には、ExceptionResolvers、RequestHandlerとRequestAdapterの場合は
JAN
解決
最も直接的な方法は、WebMvcConfigurationSupport
を拡張することです。メソッドをオーバーライドすることで、ほとんどすべてがすべてを設定できます。
しかしそれはものを設定するための非常に直接的な方法であることに注意してください。それはあなたが今、あなたが持っているよりもはるかにコントロールを与えますが、WebMvcConfigurerAdapter
もあなたに与えるでしょう。ドキュメントから:
If the customization options of {@link WebMvcConfigurer} do not expose
something you need to configure, consider removing the {@code @EnableWebMvc}
annotation and extending directly from {@link WebMvcConfigurationSupport}
overriding selected {@code @Bean} methods
.
カスタム(またはカスタマイズされた)メッセージコンバータは、configureMessageConverters
をオーバーライドすることによって追加できます。
他のヒント
webmvcconfigurerAdapter あなたのWeb構成のために、少し魔法を感じるべきであり、あなたのメッセージ変換器と他の多くのコンポーネントを構成するためのフックを与えるべきです。
@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter
{
@Autowired
private CustomObjectMapper domainMapper;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(domainMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
}
.