Unable to setup static website on Spring Boot - java

According to Spring Boot Documentation
By default Spring Boot will serve static content from a directory called /static in the classpath or from the root of the ServletContext
I've set up spring.mvc.static-path-pattern=/resources/** in the application.properties - nonetheless I get an error 404.
(No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcherServlet')
Here is my project hierarchy:
What am I missing?
Solution:
I had a Controller Mapping to /, after removing it I could see the website! facepalm

Did you try it without the spring.mvc.static-path-pattern=/resources/**? Or try to access /static/index.html with the property set? Because that setting changes where Spring Boot would look for static files, and I clearly see your index.html being in resources/static folder.

Related

Spring Boot not working with dependent application.properties

I'm working with the Spring Boot 2.2.9.RELEASE. I have the main app and some plain starter (which just uses spring-actuator functionality) with some properties in its some-starter/src/main/resources/application.properties file:
management.server.port=9000
management.server.ssl.enabled=false
management.endpoints.enabled-by-default=false
management.endpoint.health.enabled=true
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=health
I've imported the starter into my main app and I believe that the healthcheck endpoint should work with the port 9000 and with the path /health (smth like that localhost:9000/health).
But it doesn't. However, it works in case of the same properties in my main app main-app/src/main/resources/application.properties.
Is it problem with the property overriding in Spring Boot? Should i configure my resources via something like maven-resources-plugin?
When application.properties is loaded from the classpath, the first one on the classpath is loaded and any others on the classpath are ignored. In your case, the file in main-app/src/main/resources/application.properties will appear on the classpath before the application.properties in the jar of some-starter.
As its name suggests, application.properties is intended for configuring your application and it shouldn't be used in a starter. You should either configure all of the properties in your application, or you could update your starter to include an EnvironmentPostProcessor that is registered via spring.factories and adds some default properties to the Environment.

Spring Boot with custom context path - Cannot access static web files

I have a Spring Boot application (2.2.6.RELEASE) that uses ReactJs as a frontend library.
I have configured in application.properties a custom context-path and spring.mvc properties like so:
server.servlet.context-path=/gui
spring.mvc.view.prefix: /static/dist/
spring.mvc.view.suffix: .html
spring.mvc.static-path-pattern=/static/**
Webpack is used to build bundles and an index.html into src/main/resources/static/dist. Here is how the project structure looks like:
I need to be able to access index.html from
localhost:8080/gui
with these settings but for some reason it does not pick it up. However if I try with
localhost:8080/gui/static/dist/index.html
the resource is reached. How do I configure Spring to serve the resources as I would like to?
#Oleh Kurpiak answer was correct. Using spring.resources.static-locations=classpath:/static/dist/ helped out.

server.servletPath=/* not working in spring-boot.version 2.1.7.RELEASE

I have been using the below property in the application.properties file with spring-boot.version 1.5.6.RELEASE without any issues.
server.servletPath=/*
This was a workaround to enable a method in a library class which uses the function getPathInfo() of javax.servlet.http.HttpServletRequest to get a valid value instead of null.
I had to go with this workaround since there is no support of that library jar anymore.
This workaround started failing when I upgraded my application to spring-boot.version 2.1.7.RELEASE
server.servletPath is changed to spring.mvc.servletPath from Spring Boot 2 onwards.
I tried setting the below property and it did not work
spring.mvc.servletPath=/*
I also tried the below function in my configuration class and it did not work.
#Bean
public DispatcherServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet,
ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
dispatcherServlet, "/*");
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(-1);
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
Could you please provide a working solution for this property using spring-boot.version 2.1.7.RELEASE?
Thanks,
Dhinu
The correct setting for newer spring versions is:
spring.mvc.servlet.path=/some/path
This changes the mapping of the DispatcherServlet, so all resources served by spring are mapped to this path.
If you set:
server.servlet.contextPath=/some/path
The whole web context is changed.
The main difference is that setting the dispatcher servlet path allows you to register additional servlets on other paths while with context path set, spring boot's tomcat can only serve content below that context path.
Use the following config property on latest spring boot version:
server.servlet.contextPath=/*

How to bypass contextpath for health.html page. Our requirement is to have health.html under classpath directly

We have a Spring Boot application where context path is defined and it is deployed in PCF as jar(please note it is not WAR file). But, our load balancer keeps checking for health.html. So I created a health.html under src/main/resources/static folder.
For eg : server.servlet.context-path = /TransactionManagement.
But this health.html is accessible via https://myapplication.abc.com/TransactionManagement/health.html instead of https://myapplication.abc.com/health.html.
How to bypass contextPath in this case?

My spring boot with Gradle giving me a null modelAndView object

So this is my first web project with spring boot and Gradle, and I have done the same steps as mentioned in the get started with spring boot.
Even though the building process is ok and the dependencies seems to be also ok, however whenever I call the controller /firstapp I get a null view, even if I tried to return a string I have a Whitelabel Error Page.
This is my project.
Project directory picture:
Test controller :
main class :
Application.properties :
build.gradle :
Try the *.html in the spring.mvc.view.suffix property instead of .html
The problem was due to my bad directory configuration, the spring-boot could not read the Viewresolver from the application.properties and also the controller class should be in a subdirectory under the main springapplication.

Categories