- Create a Spring Boot Project with the Spring Web dependency.
- Consider some model requirements.
- Review the project structure.
In a similar fashion as before, let's create a Spring Boot project and add the Spring Web dependency to create a Spring MVC!
- Go to https://start.spring.io/.
- Select the project properties.
- Select "Maven Project", as we will use Maven as the build tool.
- Select "Java" as the language.
- Select the most recent version of Spring Boot 2. (Make sure it does not have "SNAPSHOT" listed after it.)
- Change the "Artifact" metadata field to "spring-web-demo". (This will update the "Name" and "Package name" metadata fields too).
- Change the "Description" metadata field to "Demo project for Spring Web".
- Select the appropriate Java JDK version.
- Add dependencies.
- Let's add the Spring Web dependency to create a Spring web application.
- Click "ADD DEPENDENCIES".
- Search for "spring web".
- Select "Spring Web" from the list.
- Click on the "Generate" button on the bottom. This will download a zip file containing the Spring Boot Project.
- Unzip the archive and open it in IntelliJ or a preferred code editor.
Spring Boot provides default configurations that set up the web server which handles the communication layer. We can focus on the business logic and let Spring Boot handle most of the boilerplate configurations.
Spring Web bundles several dependencies that make it easy to build a web
application. We'll see how helpful this is to us in a couple of lessons! In the
meantime, we can see that our Spring Web dependency has been added by looking in
the pom.xml file under <dependencies>
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Remember from the previous lesson that the model is the data and its relationships we would like to represent in a web application. So let's pick something to model!
Since this is our first API we are creating, let's start simple. Consider a cafeteria. We will create the following options:
- Greet the customer.
- Tell the customer the daily lunch special.
- Thank the customer for coming into our cafeteria.
Great! Now let's set this up in our project!
Create the following packages in the com.example.springwebdemo
package:
controller
service
Under the controller
package, create a file called LunchController.java
. Then
create a LunchService.java
file under the newly created service
package.
Your project structure should look like this:
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── springwebdemo
│ │ ├── SpringWebDemoApplication.java
│ │ ├── controller
│ │ │ └── LunchController.java
│ │ └── service
│ │ └── LunchService.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── org
└── example
└── springwebdemo
└── SpringWebDemoApplicationTests.java
Congratulations! We have set up the basic structure of our application. Now we created two packages with two classes: a controller and a service. But what are those classes? Let's find out in the next lessons!