Commit 0bc5c2ba authored by Andy Wilkinson's avatar Andy Wilkinson

Ensure that containers' static resource handling not MVC's is used

Closes gh-25949
parent 709db558
...@@ -11,7 +11,6 @@ configurations { ...@@ -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 { ...@@ -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"))
......
/* /*
* 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 @@ ...@@ -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;
}
@Bean
public ServletRegistrationBean<TestServlet> testServlet() {
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
registration.addUrlMappings("/always");
return registration;
} }
@Endpoint(id = "war") static class TestServlet extends HttpServlet {
static class TestEndpoint {
@ReadOperation @Override
String hello() { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
return "{\"hello\":\"world\"}"; resp.setContentType(MediaType.APPLICATION_JSON_VALUE);
resp.getWriter().println("{\"hello\":\"world\"}");
resp.flushBuffer();
} }
} }
......
...@@ -56,9 +56,17 @@ public class ResourceHandlingApplication { ...@@ -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 {
......
/* /*
* 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 { ...@@ -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 {
......
/* /*
* 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 { ...@@ -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);
} }
......
/* /*
* 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 { ...@@ -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) {
......
...@@ -18,11 +18,7 @@ ...@@ -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 @@ ...@@ -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>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment