Giter VIP home page Giter VIP logo

spring-mod-1-mvc-lab's Introduction

Spring Web MVC Lab

Learning Goals

  • Practice creating a web application using the Spring Framework.
  • Create a @RestController class.
  • Create a @Service class.
  • Use Postman as an API Client tool to test.

Instructions

Create a simple web application using the Spring Framework to perform simple calculator operations between two numbers.

Follow the given instructions and tips:

  • Create a CalculatorController class. This will be the controller class.
    • The controller will need to have the following methods:
      • add
      • subtract
      • multiply
      • divide
    • Each of the methods above should take in two double variables as request parameters.
    • Each of the methods above should return a Double.
    • Each of these methods will use the @GetMapping annotation with the method name as the path (i.e., "/add" will be the route for the add() method).
      • Since each of these methods will take in two parameters, the full URL might look like: http://localhost:8080/add?x=8&y=4 where x and y are the request parameters.
  • Create a CalculatorService class. This will be where we place the business logic.
    • The service will need to have the following methods:
      • sum
      • difference
      • product
      • quotient
    • Each of the methods above should take in two double variables as parameters and return a Double.
    • For the subtract() and divide() methods, the first number argument will be the minuend or the dividend respectfully.
      • If the first parameter in a subtraction method is 5, then 5 will be the number we subtract from.
      • If the first parameter in a division method is 5, then 5 will be the number that is being divided.
    • Remember, anything divided by 0 is not a number. Print an error to the console and return null when 0 is given as divisor (second number).
    • Add the service to the controller class as we saw in the Service Class lesson.
      • Add a private final reference to the CalculatorService class to the controller class.
      • Add an @Autowired constructor to the controller class.
  • Suggestion: Start by implementing the add() and sum() methods in the controller and the service classes respectively. Once that is working, then add the other methods one at a time.
  • Use the SpringMod1MvcLabApplication class to run the application.
  • Use Postman to test the application.

Project Structure

The Spring Boot project has already been initialized for you. Consider the following project structure:

├── CONTRIBUTING.md
├── HELP.md
├── LICENSE.md
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── springmod1mvclab
    │   │               ├── SpringMod1MvcLabApplication.java
    │   │               ├── controller
    │   │               │   └── CalculatorController.java
    │   │               └── service
    │   │                   └── CalculatorService.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── org
                └── example
                    └── springmod1mvclab
                        └── SpringMod1MvcLabApplication.java

Example Output

Consider the example outputs:

Addition

Request URL: http://localhost:8080/add?x=8.0&y=4.0

Postman-add-request

Subtraction

Request URL: http://localhost:8080/subtract?x=8.0&y=4.0

Postman-subtract-request

Multiplication

Request URL: http://localhost:8080/multiply?x=8.0&y=4.0

Postman-multiply-request

Division

Request URL: http://localhost:8080/divide?x=8.0&y=4.0

Postman-divide-request

Request URL: http://localhost:8080/divide?x=8&y=0

Note that request above will attempt to divide by 0; which should return null since anything divided by 0 is not a number.

Postman-divide-by-zero

Console-error-divide-by-0

spring-mod-1-mvc-lab'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.