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 {
dependencies {
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("com.samskivert:jmustache")
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-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-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-json", 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");
* you may not use this file except in compliance with the License.
......@@ -16,27 +16,44 @@
package com.autoconfig;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import java.io.IOException;
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.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
@ConditionalOnWarDeployment
@Configuration
public class ExampleAutoConfiguration {
@Bean
public TestEndpoint testEndpoint() {
return new TestEndpoint();
@ConditionalOnWarDeployment
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 TestEndpoint {
static class TestServlet extends HttpServlet {
@ReadOperation
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,8 +56,16 @@ public class ResourceHandlingApplication {
}
public static void main(String[] args) {
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0")
.listeners(new WebServerPortFileWriter(args[0])).run(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")
.listeners(new WebServerPortFileWriter(args[0])).run(args);
}
}
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");
* you may not use this file except in compliance with the License.
......@@ -161,8 +161,6 @@ class ApplicationBuilder {
metaInf.mkdirs();
FileCopyUtils.copy(new File("src/test/resources/META-INF/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 {
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -81,7 +81,7 @@ class EmbeddedServletContainerJarPackagingIntegrationTests {
@TestTemplate
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);
}
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -104,8 +104,9 @@ class EmbeddedServletContainerWarPackagingIntegrationTests {
@TestTemplate
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(rest.getForEntity("/always", String.class).getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(rest.getForEntity("/conditionalOnWar", String.class).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
private List<String> readLines(String input) {
......
......@@ -18,11 +18,7 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -30,7 +26,7 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<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