Ensure that containers' static resource handling not MVC's is used
Closes gh-25949
This commit is contained in:
parent
709db5582b
commit
0bc5c2ba8c
|
|
@ -11,7 +11,6 @@ configurations {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||||
testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
|
|
||||||
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
||||||
testImplementation("com.samskivert:jmustache")
|
testImplementation("com.samskivert:jmustache")
|
||||||
testImplementation("jakarta.servlet:jakarta.servlet-api")
|
testImplementation("jakarta.servlet:jakarta.servlet-api")
|
||||||
|
|
@ -26,7 +25,6 @@ dependencies {
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository"))
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "mavenRepository"))
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository"))
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator", configuration: "mavenRepository"))
|
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-jetty", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-jetty", configuration: "mavenRepository"))
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-json", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-json", configuration: "mavenRepository"))
|
||||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-parent", configuration: "mavenRepository"))
|
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-parent", configuration: "mavenRepository"))
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,27 +16,44 @@
|
||||||
|
|
||||||
package com.autoconfig;
|
package com.autoconfig;
|
||||||
|
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
import java.io.IOException;
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
|
||||||
@ConditionalOnWarDeployment
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class ExampleAutoConfiguration {
|
public class ExampleAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TestEndpoint testEndpoint() {
|
@ConditionalOnWarDeployment
|
||||||
return new TestEndpoint();
|
public ServletRegistrationBean<TestServlet> onWarTestServlet() {
|
||||||
|
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
|
||||||
|
registration.addUrlMappings("/conditionalOnWar");
|
||||||
|
return registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Endpoint(id = "war")
|
@Bean
|
||||||
static class TestEndpoint {
|
public ServletRegistrationBean<TestServlet> testServlet() {
|
||||||
|
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
|
||||||
|
registration.addUrlMappings("/always");
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
|
||||||
@ReadOperation
|
static class TestServlet extends HttpServlet {
|
||||||
String hello() {
|
|
||||||
return "{\"hello\":\"world\"}";
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
resp.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
resp.getWriter().println("{\"hello\":\"world\"}");
|
||||||
|
resp.flushBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,9 +56,17 @@ public class ResourceHandlingApplication {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Class.forName("org.springframework.web.servlet.DispatcherServlet");
|
||||||
|
System.err.println("Spring MVC must not be present, otherwise its static resource handling "
|
||||||
|
+ "will be used rather than the embedded containers'");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0")
|
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0")
|
||||||
.listeners(new WebServerPortFileWriter(args[0])).run(args);
|
.listeners(new WebServerPortFileWriter(args[0])).run(args);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final class GetResourcePathsServlet extends HttpServlet {
|
private static final class GetResourcePathsServlet extends HttpServlet {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -161,8 +161,6 @@ class ApplicationBuilder {
|
||||||
metaInf.mkdirs();
|
metaInf.mkdirs();
|
||||||
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
|
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
|
||||||
new File(metaInf, "spring.factories"));
|
new File(metaInf, "spring.factories"));
|
||||||
FileCopyUtils.copy(new File("src/test/resources/application.yml"),
|
|
||||||
new File(srcMainResources, "application.yml"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void packageApplication(File appDirectory, File settingsXml) throws MavenInvocationException {
|
private void packageApplication(File appDirectory, File settingsXml) throws MavenInvocationException {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -81,7 +81,7 @@ class EmbeddedServletContainerJarPackagingIntegrationTests {
|
||||||
|
|
||||||
@TestTemplate
|
@TestTemplate
|
||||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
ResponseEntity<String> entity = rest.getForEntity("/war", String.class);
|
||||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -104,8 +104,9 @@ class EmbeddedServletContainerWarPackagingIntegrationTests {
|
||||||
|
|
||||||
@TestTemplate
|
@TestTemplate
|
||||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
assertThat(rest.getForEntity("/always", String.class).getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
assertThat(rest.getForEntity("/conditionalOnWar", String.class).getStatusCode())
|
||||||
|
.isEqualTo(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> readLines(String input) {
|
private List<String> readLines(String input) {
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
management.endpoints.web.exposure.include: '*'
|
|
||||||
|
|
@ -18,11 +18,7 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-json</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|
@ -30,7 +26,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue