Java 将ViewResolverRegistry添加到SpringWebConfig后,MappingJackson2JsonView Bean中断
已解决-请参阅答案 我已经看过很多类似的问题,但没有看到一个类似的案例。当然,这不是一个独特的情况,我只是错过了它 更新这可能会有所帮助,但我只找到了XML示例。问题扩展如下 问题摘要 在我的SpringWebMVC应用程序中,两个视图解析器似乎冲突 问题详细信息 我正在使用Spring4.0.3-RELEASE开发一个web应用程序,最近添加了Jackson,以支持从调用返回Json到特定控制器。在我为ConfigureViewResolver的SpringWebConfig添加@Override之前,这一直有效。现在,我对提供Json的控制器的调用只返回应该调用Jackson映射器bean的模板名 大问题 我怎样才能使这两者共存?我发现我可以打电话:Java 将ViewResolverRegistry添加到SpringWebConfig后,MappingJackson2JsonView Bean中断,java,spring,jackson,Java,Spring,Jackson,已解决-请参阅答案 我已经看过很多类似的问题,但没有看到一个类似的案例。当然,这不是一个独特的情况,我只是错过了它 更新这可能会有所帮助,但我只找到了XML示例。问题扩展如下 问题摘要 在我的SpringWebMVC应用程序中,两个视图解析器似乎冲突 问题详细信息 我正在使用Spring4.0.3-RELEASE开发一个web应用程序,最近添加了Jackson,以支持从调用返回Json到特定控制器。在我为ConfigureViewResolver的SpringWebConfig添加@Overri
registry.order(int)
并将其设置为9以确保它是最后一个,但它仍然截获了来自控制器的jsonTemplate响应。我看不到为MappingJackson2JsonView bean设置顺序的方法@例如,Bean(order=0)是无效的
尝试过的事情
在尝试获取映射的jsp视图时,按预期编辑ViewResolverRegistry会产生错误
javax.servlet.ServletException: Could not resolve view with name 'someView' in servlet with name 'spring-mvc-dispatcher'
正如上面的问题陈述所指出的,我已经尝试在注册表上设置ViewResolverRegistry的顺序,但这没有帮助
我还尝试将以下内容添加到MappingJackson2JsonView实例视图中:
Properties props = new Properties();
props.put("order", 1);
view.setAttributes(props);
但和以前一样,这并不能阻止ViewResolverRegistry在Jackson映射程序处理“jsonTemplate”之前拦截它
我还更改了AppInitializer中配置的加载顺序,下面的代码已经更新以反映新的加载顺序,但这也没有帮助
再仔细阅读一下Spring文档,我似乎需要添加ContentNegotiationConfigurer来解决这个问题,我目前正在研究如何以一种保持自动映射模型返回到jsonTemplate视图的方式来实现这一点。到目前为止,我看到的Exapmles使用jsp作为具有特定属性的视图,这违背了使用Json映射器的目的
配置
我的包com.mytest.config中定义了多个配置类
java处理将*config类添加到上下文的操作。
包com.mytest.config
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
private Logger logger = LoggerFactory.getLogger(AppInitializer.class);
@Override
public void onStartup(ServletContext container) throws ServletException {
try {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(JSONConfiguration.class);
ctx.register(SpringWebConfig.class);
ctx.setServletContext(container);
container.addListener(new ContextLoaderListener(ctx));
container.addListener(new RequestContextListener());
logger.info("Created AnnotationConfigWebApplicationContext");
ServletRegistration.Dynamic dispatcher = container.addServlet("spring-mvc-dispatcher", new DispatcherServlet(ctx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
logger.info("DispatcherServlet added to AnnotationConfigWebApplicationContext");
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
}
}
java是我注册大部分bean的地方
package com.mytest.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"com.mytest.controller","com.mytest.bean","com.mytest.model"})
@PropertySource(value={"classpath:application.properties"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Autowired
private Environment env;
private Logger logger = LoggerFactory.getLogger(SpringWebConfig.class);
// bunches of beans such as JdbcTemplate, DataSource... omitted for simplicity
@Override // apparent problem location -- needed for jsp resolving
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/html/",".jsp");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
logger.info("DefaultServletHandlerConfigurer enabled");
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new com.honda.hrao.rid.config.RequestInterceptor());
logger.info("RequestInterceptor added to InterceptorRegistry");
}
}
java是我为JSON设置的一个控制器
package com.mytest.config;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
@Configuration
@ComponentScan(basePackages = {"com.mytest.controller"})
@EnableWebMvc
public class JSONConfiguration {
private Logger logger = LoggerFactory.getLogger(JSONConfiguration.class);
@Bean // needed for JSON conversion of bean responses
public View jsonTemplate() {
logger.info("Registered MappingJackson2JsonView");
MappingJackson2JsonView view = new MappingJackson2JsonView();
Properties props = new Properties();
props.put("order", 1);
view.setAttributes(props);
view.setPrettyPrint(true);
return view;
}
@Bean
public ViewResolver viewResolver() {
logger.info("Starting ViewResolver bean");
return new BeanNameViewResolver();
}
}
实施
在我的控制器中,以下方法应该返回JSON
@Autowired
AppConstants appConstants;
@RequestMapping(method = RequestMethod.GET, value = "getAppConstants")
public String getAppConstants(Model model) {
model.addAttribute("AppConstants",appConstants);
if(appConstants==null) {
Logger.error("appConstants not autowired!!!");
return null;
}
return "jsonTemplate";
}
正如前面在Things Tested中提到的,如果我从SpringWebConfig中删除ViewResolverRegistry bean,并且如果我将该bean保留在适当的位置,那么上述控制器方法将返回
404, /WEB-INF/views/html/jsonTemplate.jsp
The requested resource is not available.
--我明白。这就是视图解析器应该做的。如何让JSON调用绕过这个问题?事实证明,只缺少几件事。第一个是为映射器的bean声明添加以下注释:
@Primary
现在,bean设置如下所示
@Bean // needed for JSON conversion of bean responses
@Primary
public View jsonTemplate() {
logger.info("Registered MappingJackson2JsonView");
MappingJackson2JsonView view = new MappingJackson2JsonView();
Properties props = new Properties();
props.put("order", 1);
view.setAttributes(props);
view.setPrettyPrint(true);
return view;
}
第二种方法是使用ContentNegotiationConfigurer。在我的SpringWebConfig中,我添加了以下内容:
public void configurationContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.ignoreUnknownPathExtensions(false)
.defaultContentType(MediaType.TEXT_HTML);
}
并将我的ConfigureViewResolver功能更改如下:
@Override // needed for jsp resolving
public void configureViewResolvers(final ViewResolverRegistry registry) {
MappingJackson2JsonView view = new MappingJackson2JsonView();
view.setPrettyPrint(true);
registry.enableContentNegotiation(view);
registry.jsp("/WEB-INF/views/html/",".jsp");
}
其中一条线索是在一辆卡车上发现的。其余部分来自Spring文档