Jersey和Springboot应用程序抛出java.lang.StackOverflowError

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jersey和Springboot应用程序抛出java.lang.StackOverflowError相关的知识,希望对你有一定的参考价值。

[尝试使用Jersey创建一个Spring-boot应用程序以响应REST调用。代码类似于此question。该应用程序成功启动,但是当进行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.StackOverflowError: 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

用于声明servlet过滤器的注释。

以上是关于Jersey和Springboot应用程序抛出java.lang.StackOverflowError的主要内容,如果未能解决你的问题,请参考以下文章

springboot 之 JAX-RS 和 Jersey

在springboot中整合jersey和springfox-swagger2

Spring Boot Jersey 和监控 URL

Jersey REST 服务抛出超时异常

SpringBoot集成Jersey

Spring-Boot Jersey:允许 Jersey 提供静态内容