我正在启动一个现有的 Spring Web 应用程序,以便生成的 war 文件嵌入一个 Jetty Web 服务器。我想尽可能地坚持现有的配置,以限制回归。
这是现有的web.xml
:
<web-app id="fbecart-webapp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.fbecart.ApplicationConfiguration</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>com.fbecart.MyDispatcherServlet</servlet-class>
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.fbecart.SpringDispatcherServletConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我的主要课程JettyApplication.java
:
package com.fbecart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ ApplicationConfiguration.class, SpringDispatcherServletConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class })
public class JettyApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(JettyApplication.class, args);
}
}
我对我的 Gradle 构建脚本进行了一些更改以使其工作:
- 将依赖项添加到 spring-boot-starter 和 spring-boot-starter-jetty
- spring-boot插件的配置
该应用程序启动正常,控制器已加载,我可以查询服务器。但是 web.xml 中定义的过滤器都没有启用。
现在我想删除 和 in 的导入,并通过PropertiesConfiguration.class
将的内容加载或导入到嵌入式 servlet 容器中以某种方式替换它们。但我忽略了这是否是一个正确的策略,如果我能做到。我将不胜感激任何帮助。ApplicationConfiguration.class
SpringDispatcherServletConfiguration.class
JettyApplication.java
web.xml
- 解决方案
JettyApplication.class
这是基于戴夫回答的最终结果:
package com.fbecart;
import org.eclipse.jetty.servlets.GzipFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
@Configuration
@Import({ ApplicationConfiguration.class, SpringDispatcherServletConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class })
public class JettyApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(JettyApplication.class, args);
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new MyDispatcherServlet();
}
@Bean
public GzipFilter gzipFilter() {
return new GzipFilter();
}
@Bean
public CharacterEncodingFilter characterEncodingFilter() {
final CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
@Bean
public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {
return new OpenEntityManagerInViewFilter();
}
}
我将在不久的将来用 ServletContainerInitializer 替换 web.xml ......敬请期待;)