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

标签 java spring spring-boot filter jersey

尝试使用 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

Annotation used to declare a servlet filter.

关于java - Jersey 和 Springboot 应用程序抛出 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61793461/

相关文章:

java - 将 DAO 类声明为 Spring Autowiring 的类成员或方法参数?

java - spring-data-couchbase 中带有 Pageable 参数的 @Query

java - Spring:为什么要有多个缓存

java - 即使存在依赖项,导入错误

Java:比较整数之谜

java - 不兼容的数据类型错误

java - 执行前在 Java 代码中设置 Hadoop 输出文件夹复制

java - 我将如何创建对象引用变量数组?

java - @OneToMany 不获取现有的子项

spring - 如何在gradle中获取依赖项的元数据?