Skip to content

simulations v0.3

Albert edited this page Apr 25, 2024 · 7 revisions

Project Overview

In this lab we are going to:

  • Focus on project with H2 JPA and Postman
  • Test it with API Rest
  • Work on @Entity

Project structure

image

Postman to test API Rest

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.

What is Postman?

Code

Command Line Runner

CommandLineRunner is an interface that has run() method. It is used to execute some code just after the Spring Boot application has started.

The main application should implement this interface and override its run method. In this run method, we write code like initializing our database with some value or any other logic which should be executed just after the app starts.

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class RunnerFillingDB implements ApplicationRunner {
    @Autowired
    SimulationService simulationService;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        simulationService.populate();


    }
}

Rest Controller (delete)

image

    package com.example.demo;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import java.util.Optional;
 
    @RestController
    @RequestMapping("/api/v1/simulation/")
    public class SimulationRestController {
    
    @Autowired
    SimulationRepository simulationRepository;


    @DeleteMapping
    public String deleteSimulation(@RequestParam String id) {

        //System.out.println("id:" + id);
        Optional<Simulation> simulationFound = simulationRepository.findById(id);

        //System.out.println("simulationFound:" + simulationFound);

        if (simulationFound.isPresent()){
            simulationRepository.deleteById(id);
            String response = "simulation deleted: " + id;
            return response;
        } else return "id not found";

    }
}

Output

image

image

image

Clone this wiki locally