Browse Source

Only set init param to disable Jersey when Jersey is present

Fixes gh-45289
pull/45382/head
Andy Wilkinson 9 months ago
parent
commit
f1fefc5ff6
  1. 9
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java
  2. 25
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java

9
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java

@ -173,9 +173,12 @@ public class JerseyAutoConfiguration implements ServletContextAware { @@ -173,9 +173,12 @@ public class JerseyAutoConfiguration implements ServletContextAware {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// We need to switch *off* the Jersey WebApplicationInitializer because it
// will try and register a ContextLoaderListener which we don't need
servletContext.setInitParameter("contextConfigLocation", "<NONE>");
if (ClassUtils.isPresent("org.glassfish.jersey.server.spring.SpringWebApplicationInitializer",
getClass().getClassLoader())) {
// We need to switch *off* the Jersey WebApplicationInitializer because it
// will try and register a ContextLoaderListener which we don't need
servletContext.setInitParameter("contextConfigLocation", "<NONE>");
}
}
}

25
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,18 +16,25 @@ @@ -16,18 +16,25 @@
package org.springframework.boot.autoconfigure.jersey;
import java.util.Collections;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration.JerseyWebApplicationInitializer;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.filter.RequestContextFilter;
import static org.assertj.core.api.Assertions.assertThat;
@ -106,6 +113,22 @@ class JerseyAutoConfigurationTests { @@ -106,6 +113,22 @@ class JerseyAutoConfigurationTests {
.stream()
.filter(JakartaXmlBindAnnotationIntrospector.class::isInstance)).isEmpty();
});
}
@Test
void webApplicationIntializerDisablesJerseysWebApplicationInitializer() throws ServletException {
ServletContext context = new MockServletContext();
new JerseyWebApplicationInitializer().onStartup(context);
assertThat(context.getInitParameter("contextConfigLocation")).isEqualTo("<NONE>");
}
@Test
@ClassPathExclusions("jersey-spring6-*.jar")
void webApplicationInitializerHasNoEffectWhenJerseyIsAbsent() throws ServletException {
ServletContext context = new MockServletContext();
new JerseyWebApplicationInitializer().onStartup(context);
assertThat(Collections.list(context.getInitParameterNames())).isEmpty();
}
@Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save