In this article, we will explore the details of creating web applications using Spring Boot. We will explore how Spring Boot can help you accelerate application development.
We will use Spring Boot to build a simple web application and add some useful services to it.
1. Introduction
One of the main challenges in starting a new project is the initial setup of the project. We need to make calls to different directory structures and we need to make sure we follow all industry standards. For creating web applications using Spring Boot, we need the following tools:
2. Create a project structure
There are several ways to generate project structures for you using Spring Boot Initializr:
To simplify this article, we use Spring Initializer's web interface to generate the project structure.
Use your browser to access the Spring Initializr web interface and you will see a wizard to start your configuration.
You need to fill in some information in the web interface to get started.
Dependencies are an interesting feature in the web interface. Depending on the dependency you choose, the web interface will automatically add Spring Boot Starter dependencies to the generated pom.xml file. If you want more control over the generated project structure, or are not sure of all the dependencies you want to add to your project, click the "Switch to the full version" button.
In this article, we will use two Starters using Web and Thymeleaf (for user interface).
3. Project structure
Spring Boot does not require any specific code layout or structure. We can always follow some of the best practices proposed by the Spring Boot team, but the ultimate structure will be driven by your project requirements.
The following figure is the project structure of our example application:
4. pom.xml
Let's take a look at the pom.xml file to learn more about Spring Boot configuration. I will only cover Spring Boot-related changes in pom.xml. The following is the pom.xml file in our sample project.
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javadevjournal</groupId> <artifactId>javadevspringboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>javadevspringboot</name> <description>Java Dev Journal project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies></project>
One of the main features of Spring Boot is "Starter", which are an easy way to add the required dependencies (jar packages) in our classpath. When using Spring Boot, we don't have to add jar packages or dependencies in our classpath (if the starter is not available, you may have to add these dependencies to the pom.xml, or you can create your own custom starter). We just need to add the correct "Starter" in our pom.xml file and Spring Boot will ensure that these dependencies are added automatically.
5. Main application
As shown below is our Spring Boot application main class, which is also a Spring configuration class. Annotation @SpringBootApplication enables Spring context and all boot magic of Spring Boot.
@SpringBootApplicationpublic class WebApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); }}5. 1 @SpringBootApplication Annotation
@SpringBootApplication is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan and their default values. If you want to start a project, it is recommended to use this annotation.
Using @SpringBootApplication in the main class is equivalent to using the following 3 annotations at the same time:
5.2 Main method
Another interesting feature of our main class is the main method. This is a standard way to follow a standard Java workflow. Our main class will hand over control to the Spring Boot SpringApplication class.
The run method of the SpringApplication class will be used to boot an application.
6. Welcome Controller
In the last part of our setup, we will create a welcome controller that is responsible for handling /greeting's GET requests by returning the name of the View (in this case "welcome"). The view is responsible for rendering HTML content.
import org.springframework.steretype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class WelcomeController { @RequestMapping("/welcome") public String welcome() { return "welcome"; }}This is a very simple controller, but there are a lot of points covered in our setup.
6. 1 UI Template
Here is our simple Thymeleaf HTML template.
<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org"><head> <title>Getting Started: Serving Web Content</title></head><body>Hello and Welcome to our Web Application</body></html>
When using Thymeleaf as our template engine, Spring Boot will prefix and suffix before and after the view name (configuration parameters are: spring.thymeleaf.prefix and spring.thymeleaf.suffix, their default values are: 'classpath:/templates/' and 'html').
7. Run the program
We've finished our simple web application and it's time to run our application. While this service can be packaged as a traditional WAR file for deployment on an external application server, an easier way is to create a standalone application. To run our application from the IDE, we need to run our web application as a standalone Java application.
Now that the website is up and running, please visit http://localhost:8080/welcome and if everything works, you should output the following in your web browser.
Hello and Welcome to our Web Application
8. Summary
In this article, we learned to create web applications using Spring Boot. Spring Boot has many features that can create and run web applications faster and easier.
Original link: https://www.javadevjournal.com/spring/creating-a-web-application-with-spring-boot/
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.