具有两个MVC配置的Spring Boot [英] Spring Boot with Two MVC Configurations
问题描述
我有一个带有REST API的Spring Boot应用程序,使用Jackson进行JSON视图配置.它很棒,我可以得到所有Spring Boot的好处.
I have a Spring Boot app with a REST API, using Jackson for the JSON view configuration. It works great and I can get all the Spring Boot goodness.
但是,我需要添加一个相似但具有不同设置的附加REST API.例如,除其他外,它需要一个不同的Jackson对象映射器配置,因为JSON看起来会大不相同(例如,没有JSON数组).那只是一个例子,但是有很多差异.每个API都有不同的上下文(例如/api/current和/api/legacy).
However, I need to add an additional REST API that is similar but with different settings. For example, among other things, it needs a different Jackson object mapper configuration because the JSON will look quite a bit different (e.g. no JSON arrays). That is just one example but there are quite a few differences. Each API has a different context (e.g. /api/current and /api/legacy).
理想情况下,我希望将两个MVC配置映射到这些不同的上下文,而不必放弃启动时的任何自动连线.
Ideally I'd like two MVC configs mapped to these different contexts, and not have to give up any of the automatic wiring of things in boot.
到目前为止,我能够使用的两个调度程序servlet各自具有自己的MVC配置,但是,这导致Boot丢弃了我自动获得的一堆东西,并且基本上消除了使用boot的原因.
So far all I've been able to get close on is using two dispatcher servlets each with its own MVC config, but that results in Boot dropping a whole bunch of things I get automatically and basically defeats the reason for using boot.
我无法将应用拆分为多个应用.
I cannot break the app up into multiple apps.
答案您无法使用Boot来执行此操作,而仍然获得其所有魔力"是一个可以接受的答案.似乎它应该能够解决这个问题.
The answer "you cannot do this with Boot and still get all its magic" is an acceptable answer. Seems like it should be able to handle this though.
推荐答案
详细介绍我昨天的评论和@Ashoka标头的想法,我建议为自定义媒体类型注册2个MessageConverters(旧版和最新版).您可以这样操作:
Expanding on my comment of yesterday and @Ashoka Header idea i would propose to register 2 MessageConverters (legacy and current) for custom media types. You can do this like that:
@Bean
MappingJackson2HttpMessageConverter currentMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
// set features
jsonConverter.setObjectMapper(objectMapper);
jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("json", "v2")));
return jsonConverter;
}
@Bean
MappingJackson2HttpMessageConverter legacyMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
// set features
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
请注意其中一个转换器的自定义媒体类型.
Pay attention to the custom media-type for one of the converters.
如果愿意,可以使用拦截器将@Ashoka提议的Version-Header重写为自定义Media-Type,如下所示:
If you like , you can use an Interceptor to rewrite the Version-Headers proposed by @Ashoka to a custom Media-Type like so:
public class ApiVersionMediaTypeMappingInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
try {
if(request.getHeader("X-API-Version") == "2") {
request.setAttribute("Accept:","json/v2");
}
.....
}
}
这可能不是您要找的确切答案,但也许可以提供一些启发.像这样注册拦截器.
This might not be the exact answer you were looking for, but maybe it can provide some inspiration. An interceptor is registered like so.
这篇关于具有两个MVC配置的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!