如何使用Spring Boot将文件放在servlet上下文路径上?

时间:2014-07-12 12:37:46

标签: java spring-boot embedded-tomcat-7

我正在尝试将BlazeDS添加到Spring Boot应用程序中。我在我的配置中添加了`MessageBrokerServlet':

@Bean
public ServletRegistrationBean messageBrokerRegistration()
{
    ServletRegistrationBean registration = new ServletRegistrationBean(new MessageBrokerServlet(), "/messagebroker/*");
    Map<String,String> params = Maps.newHashMap();
    params.put( "services.configuration.file", "/WEB-INF/flex/services-config.xml" );
    registration.setInitParameters(params);
    return registration;
}

servlet被加载,但在运行时失败:

MessageBrokerServlet in application 'undefined' failed to initialize due to runtime exception:   
Exception: flex.messaging.config.ConfigurationException: Please specify a 
valid 'services.configuration.file' in web.xml. You specified '{0}'.
This is not a valid file system path reachable via the app server and 
is also not a path to a resource in your J2EE application archive.

查看BlazeDS的源代码,我发现最终代码使用ServletContext#getResourceAsStream(path)。如何让Spring Boot中的嵌入式tomcat在该调用中返回一些内容?

我正在使用Spring Boot 1.1.1,它使用嵌入式Tomcat 7.0.54

2 个答案:

答案 0 :(得分:0)

Servlet上下文资源至少应该在战争中运行。将它们粘贴在src/main/webapp中(对于标准构建布局)。

答案 1 :(得分:0)

我最近使用以下方法做到了这一点:

@Lazy
@Bean(name="messageBroker")
public MessageBrokerFactoryBean messageBrokerFactoryBean() {
    MessageBrokerFactoryBean messageBrokerFactory = new MessageBrokerFactoryBean();
    messageBrokerFactory.setServicesConfigPath("classpath:/flex/services-config.xml");
    return  messageBrokerFactory;
}

其中services-config.xml(以及我的其他flex配置文件)位于src / main / resources / flex

希望这个希望别人!