PS: The content of this repository is based on DevSuperior's "Intensivão - Java Spring"! 💻
- The Game entity
(
entities/Game.java) create a simple temporary H2 database table, called "tb_game", using ORM (Object-Relational Mapping).
@Entity
@Table(name = "tb_game")
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@Column(name = "game_year")
private Integer year;
private String genre;
private String platforms;
private Double score;
private String imgUrl;
@Column(columnDefinition = "TEXT")
private String shortDescription;
@Column(columnDefinition = "TEXT")
private String longDescription; -
The file
import.sqlis used to insert content into this table. -
The
repositories/GameRepository.javauses the JpaRepository interface to manipulate data from database (Game.java).JpaRepository<Game, Long>
-
This project uses DTO (Data Transfer Object), that "filter" content from entity, hiding, for example, sensitive data. It happens at
GameMinDTO.java.
private Long id;
private String title;
private Integer year;
private String imgUrl;
private String shortDescription; - The main service
(
services/GameService.java) uses thefindAll()method, from JpaRepository, to return all entities from the main entity. After this, it uses the response as parameter for a new DTO object, and return that as a list.
List<Game> result = gameRepository.findAll(); // catch all entities
return result.stream().map(x -> new GameMinDTO(x)).toList();- The main controller
(
controllers/GameController.java) return all entities from the list returned fromGameService.javausingfindAll()method and map it into a route called/games.
@RestController
@RequestMapping(value = "/games") // route
public class GameController {
@Autowired
private GameService gameService;
@GetMapping
public List<GameMinDTO> findAll() {
List<GameMinDTO> result = gameService.findAll(); // return all DTO entities
return result;
}
}- Firstly, clone this repository:
git clone https://github.com/Gustanol/games_api.git && cd games_api-
Secondly, make sure you have Maven installed. If you haven't yet, follow the instructions in this page.
-
Thirdly, execute the command:
mvn clean install- After this, you can run the app by running:
mvn spring-boot:run- Now, access http://localhost:8080/games and see the API! 🤓
PS: If you prefer, you can run it into an IDE as well!