Jersey 和 Spring Boot 应用程序抛出 java.lang.StackOverflowError
Jersey and Springboot application throws java.lang.StackOverflowError
正在尝试使用 Jersey 创建 Spring-boot 应用程序以响应 REST 调用。代码类似于此。应用程序成功启动,但当 REST 调用时,如日志中所示,出现无限循环,一段时间后抛出异常。
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan
base-package="org.ideoholic" />
<context:annotation-config />
<!-- <mvc:annotation-driven /> -->
</beans>
Java配置代码:
@Configuration
@Import({ WebXmlConfiguration.class })
@ImportResource({ "classpath*:appContext.xml" })
@EnableAutoConfiguration
public class ApplicationConfiguration {
}
@Configuration
@Profile("basicauth")
public class WebXmlConfiguration {
@Bean
public Filter springSecurityFilterChain() {
return new DelegatingFilterProxy();
}
@Bean
public ServletRegistrationBean<Servlet> jersey() {
Servlet jerseyServlet = new SpringServlet();
ServletRegistrationBean<Servlet> jerseyServletRegistration = new ServletRegistrationBean<Servlet>();
jerseyServletRegistration.setServlet(jerseyServlet);
jerseyServletRegistration.addUrlMappings("/rest/v1/*");
jerseyServletRegistration.setName("jersey-servlet");
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
jerseyServletRegistration.addInitParameter("com.sun.jersey.config.feature.DisableWADL", "true");
jerseyServletRegistration.addInitParameter("com.sun.jersey.config.property.packages", "org.ideoholic");
return jerseyServletRegistration;
}
}
@SpringBootApplication
public class RestSpringApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RestSpringApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationConfiguration.class, args);
}
}
日志中看到的错误是一个无限循环 DelegatingFilterProxy
ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause
java.lang.WhosebugError: null
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
完整代码可在 Github
删除 springSecurityFilterChain()
,如果您需要添加过滤器,请使用 @WebFilter
Annotation used to declare a servlet filter.
正在尝试使用 Jersey 创建 Spring-boot 应用程序以响应 REST 调用。代码类似于此
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan
base-package="org.ideoholic" />
<context:annotation-config />
<!-- <mvc:annotation-driven /> -->
</beans>
Java配置代码:
@Configuration
@Import({ WebXmlConfiguration.class })
@ImportResource({ "classpath*:appContext.xml" })
@EnableAutoConfiguration
public class ApplicationConfiguration {
}
@Configuration
@Profile("basicauth")
public class WebXmlConfiguration {
@Bean
public Filter springSecurityFilterChain() {
return new DelegatingFilterProxy();
}
@Bean
public ServletRegistrationBean<Servlet> jersey() {
Servlet jerseyServlet = new SpringServlet();
ServletRegistrationBean<Servlet> jerseyServletRegistration = new ServletRegistrationBean<Servlet>();
jerseyServletRegistration.setServlet(jerseyServlet);
jerseyServletRegistration.addUrlMappings("/rest/v1/*");
jerseyServletRegistration.setName("jersey-servlet");
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
jerseyServletRegistration.addInitParameter("com.sun.jersey.config.feature.DisableWADL", "true");
jerseyServletRegistration.addInitParameter("com.sun.jersey.config.property.packages", "org.ideoholic");
return jerseyServletRegistration;
}
}
@SpringBootApplication
public class RestSpringApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RestSpringApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationConfiguration.class, args);
}
}
日志中看到的错误是一个无限循环 DelegatingFilterProxy
ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause
java.lang.WhosebugError: null
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
完整代码可在 Github
删除 springSecurityFilterChain()
,如果您需要添加过滤器,请使用 @WebFilter
Annotation used to declare a servlet filter.