📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Video
This tutorial is explained in the below Youtube Video. Subscribe to my youtube channel to learn more about Spring boot at Java Guides - YouTube Channel.As per Spring boot documentation, the team says Spring Boot does not require any specific code layout to work. However, there are some best practices that help.
Don't use the “default” Package
Typical Layout
Tip
Project Structure - First approach
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Static Content
Let's look at the resources folder in the above project structure. This directory, as the name suggests, is dedicated to all the static resources, templates and property files.
-
resources/static - contains static resources such as CSS, js, and images.
-
resources/templates - contains server-side templates which are rendered by Spring.
-
resources/application.properties - This file is very important. It contains application-wide properties. Spring reads the properties defined in this file to configure your application. You can define a server’s default port, server’s context path, database URLs etc, in this file.
By default, Spring boot serves static content from one of the following locations in the classpath:
- /static
- /public
- /resources
- /META-INF/resources
By default, resources are mapped on /**, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows:
spring.mvc.static-path-pattern=/resources/**
You can also customize the static resource locations by using the spring.resources.static-locations property (replacing the default values with a list of directory locations). The root Servlet context path, "/", is automatically added as a location as well.
Dynamic web content (Templates)
Spring supports the following template engines by default. These templates can be activated using appropriate spring boot starters.
- FreeMarker - spring-boot-starter-freemarker
- Groovy - spring-boot-starter-groovy
- Thymeleaf - spring-boot-starter-thymeleaf
- Mustache - spring-boot-starter-mustache
All these template engines will resolve their template files from the path src/main/resources/template.
Read more about how to serve static content in spring boot application at Spring Boot Official Doc - Static Content
- /static
- /public
- /resources
- /META-INF/resources
Dynamic web content (Templates)
Spring supports the following template engines by default. These templates can be activated using appropriate spring boot starters.- FreeMarker - spring-boot-starter-freemarker
- Groovy - spring-boot-starter-groovy
- Thymeleaf - spring-boot-starter-thymeleaf
- Mustache - spring-boot-starter-mustache
Project Structure - Second approach
pom.xml
What is the Spring Boot Starter Parent and How to use it?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
- Configuration - Java Version and Other Properties
- Dependency Management - Version of dependencies
- Default Plugin Configuration
Read more about all spring boot starters at Important Spring Boot Starters with Examples.
For a maintainable code:
- Only add needed starters. This keeps the application lighter. An unwanted starter may lead to extra autowired beans.
- Most of the starters ship with their own transitive dependencies. So you may never need to specify versions. Most of the IDE’s already highlight this unwanted version tags.
- Know your starter dependencies. This way, You may never need to write configurations.
Spring boot web applications - using JSP as views
If you are creating spring boot MVC web application using JSP as views then following diagram shows a typical project structure layout. As a best practice, I strongly encourage placing your JSP files in a directory under the 'WEB-INF' directory so there can be no direct access by clients.Check out all spring boot articles, guides, and tutorials at Top Spring Boot Tutorials
Comments
Post a Comment
Leave Comment