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 {
|
||||
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"))
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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;
|
||||
}
|
||||
|
||||
@Endpoint(id = "war")
|
||||
static class TestEndpoint {
|
||||
@Bean
|
||||
public ServletRegistrationBean<TestServlet> testServlet() {
|
||||
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
|
||||
registration.addUrlMappings("/always");
|
||||
return registration;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
String hello() {
|
||||
return "{\"hello\":\"world\"}";
|
||||
static class TestServlet extends HttpServlet {
|
||||
|
||||
@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 {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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 {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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) {
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
management.endpoints.web.exposure.include: '*'
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue