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

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.

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.

Unable to setup static website on Spring Boot

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.

configure embedded tomcat context docBase and path in spring boot gradle

How do I configure<Context docBase="{C:/..}" path="{path}"/> in spring boot gradle? i usually configure it in server.xml of external tomcat, how do I configure it via #Configuration file or in application.properties? Thank you.
You may try use this:
tomcat.addWebapp("/path", "{C:/..}");
It is not exactly what addContent does, but might help you get your resources available under /path.

Java MVC project in Spring Tool Suite additional files

I'm learning to make Java MVC project using Spring Tool Suite tool.
The path to make new project is:
File->New->SpringLegacyProject->Spring MVC Project.
My question is: which directory I have to use to add additional not-Spring files and where and what do I have to type for Spring files to see them?
For example:
css files - where to put and how to make jsp views see them, will 'link rel="" 'tag be enough?
properties files used to specify database connection or to specify messages for ReloadableResourceBundleMessageSource. In this case, do I have to create bean for this class in root-context.xml?
Thanks.
You should probably use Spring Boot (i.e. use File->New->Spring Starter Project and select Web as a starter. Place your web resources under src/main/resources/static folder. They are picked up automatically from that folder.
You should try an example project: File -> New -> Import Spring Getting Started Content and then pick "Serving Web Content" from the list.
Try some DB getting started content example to get the answer for the second part of your question.

Spring boot serving static resource

I work on spring boot application. I'm trying to serve static content with spring.
want to serve a resource stored in the /c:/frontend/files/ directory whenever a request comes in for the URL matching the pattern: /file/**:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/file/**")
.addResourceLocations("file:///C:/frontend/files/" );
}
but when i try to access to this resource using this url: http://localhost:9999/file/app.min.js
I have this problem
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
I resolved the problem. it's related to "spring-cloud-config-server". I just delete this config: org.springframework.cloud spring-cloud-config-server
It sounds like your project's folder structure is wrong.
Code should go under src/main/java and resources (like your javascript) should go under src/main/resources. You have a few different options where you can actually serve the files from. This post on the spring.io blog has the following to say:
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
Another option you also have is using webjars.
Personally, I've found it easiest to put those kind of files under src/main/resources/public. It always works without any issues for me. The interesting thing is you can put a folder named /public anywhere in your project and spring-boot will serve files out of it. You have to be really careful that it's under src/main/resources/public though if you're using a build tool like maven, as when you come to build your .jar the files won't be in the right place otherwise.

Categories