I have child servlets on different ports (i.e. port 8080 to servlet1
, port 8081 to servlet2
, ...) on Spring Boot 2.5.9. However, once I upgrade to 2.6.3, I am not able to access to the child servlets even though the servlets are starting correctly.
Here is my setup:
// App.java
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder()
.parent(Config.class)
.web(WebApplicationType.NONE)
.child(Servlet1.class)
.web(WebApplicationType.SERVLET)
.child(Servlet2.class)
.web(WebApplicationType.SERVLET)
.child(Servlet3.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
// Config.java
@Configuration
@ComponentScan("...")
@ConfigurationPropertiesScan
public class Config {
// ...
}
// Servlet1.java, Servlet2.java, Servlet3.java
@Configuration
@ComponentScan("...")
@PropertySource("classpath:servlet1/servlet1.properties") // servlet2, servlet3
@EnableAutoConfiguration
public class Servlet1 { // Servlet2, Servlet3
// ...
}
# File: `src/main/resources/servlet1/servlet1.properties`
server.port=8080
server.servlet.context-path=/
spring.resources.static-locations=/servlet1/foo
spring.application.admin.enabled=false
spring.application.admin.jmx-name=org.springframework.boot:type=servlet1,name=servlet1app
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# File: `src/main/resources/servlet1/servlet2.properties`
server.port=8081
... same as above (with different name) ...
# File: `src/main/resources/servlet1/servlet3.properties`
server.port=8082
... same as above (with different name) ...
With the above code setup, I have servlet1 at port 8080, servlet2 at port 8081, and servlet3 at port 8082.
But here is the thing - my project is Single Page Application (SPA). Therefore, I have a special setup for that:
// HomeController.java (for each servlet)
@Controller
public class HomeController {
@RequestMapping(value={"/**/{[path:[^\\.]*}"}, method=RequestMethod.GET)
public String defaultRoute(HttpServletRequest request) {
return "forward:/";
}
}
As you see, to use /**/{[path:[^\\.]*}
, I have added spring.mvc.pathmatch.matching-strategy=ant_path_matcher
in the properties
files (without this, the 2.6.x app won't start up).
Once I upgrade to 2.6.3
, it seems the child servlets start up fine - however, when I go to localhost:8080
(or 8081, 8082), it doesn't hit HomeController.java
, giving 404 error. Moreover, it seems the child servlet doesn't recognize its port number.
I am not sure what I've missed - please help me out.
Thank you, Spring Boot dev team!
Comment From: mbhave
@LouisWayne In order for us to be able to reproduce the issue you've described, can you please turn the code snippets into a minimal sample that we can run ourselves? Copy-pasting snippets can lead to errors which might not result in accurate diagnosis of the problem. You can share such a sample with us by zipping it up and attaching it to this issue or pushing it to a separate repository on GitHub.
Comment From: LouisWayne
Thank you for the reply @mbhave !! I will provide the sample code in a few days!
Comment From: LouisWayne
Thanks for waiting, @mbhave ! Please see the attached sample source code to reproduce the issue.
You can reproduce the issue by modifying <version>2.5.9</version>
in pom.xml
- and the main file is App.java
. Please let me know if you have any questions.
Thank you, Spring Boot devs!!!!!
Comment From: wilkinsona
Thanks for the sample. The property that you are using to configure your static resource location is incorrect with Spring Boot 2.6. It should be spring.web.resources.static-locations
. The property that you are using has been deprecated since 2.4. Support was removed entirely in 2.6. Making this change in servlet1.properties
, servlet2.properties
, and servlet3.properties
allows each child web application to serve its welcome page.
Comment From: LouisWayne
@wilkinsona , thank you so much for your time!
Yes, once I changed it from spring.resources.static-locations=/servlet1/dist/
to spring.web.resources.static-locations=/servlet1/dist/
, everything works fine!
Thank you for making & updating this wonderful open source project. Long live Spring Boot!
♡