Code Monkey home page Code Monkey logo

java-mod-6-create-spring-web-project's Introduction

Create Spring Web Project

Learning Goals

  • Create a Spring Boot Project with the Spring Web dependency.
  • Consider some model requirements.
  • Review the project structure.

Create the Spring Boot Project

In a similar fashion as before, let's create a Spring Boot project and add the Spring Web dependency to create a Spring MVC!

  1. Go to https://start.spring.io/.
  2. Select the project properties.
    1. Select "Maven Project", as we will use Maven as the build tool.
    2. Select "Java" as the language.
    3. Select the most recent version of Spring Boot 2. (Make sure it does not have "SNAPSHOT" listed after it.)
    4. Change the "Artifact" metadata field to "spring-web-demo". (This will update the "Name" and "Package name" metadata fields too).
    5. Change the "Description" metadata field to "Demo project for Spring Web".
    6. Select the appropriate Java JDK version.
  3. Add dependencies.
    1. Let's add the Spring Web dependency to create a Spring web application.
    2. Click "ADD DEPENDENCIES".
    3. Search for "spring web".
    4. Select "Spring Web" from the list.
  4. Click on the "Generate" button on the bottom. This will download a zip file containing the Spring Boot Project.
  5. Unzip the archive and open it in IntelliJ or a preferred code editor.

Spring Initializr settings for Spring Web project

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>

The Model Requirements

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!

Project Structure

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

Conclusion

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!

References

java-mod-6-create-spring-web-project's People

Contributors

alveem avatar kcleland0818 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.