Basically what the title says. No errors show in the console so I have no idea what's wrong. I am using CrudRepository. The task is to make a REST API for a game store. I am trying to access localhost:8080/game URL but 404 keeps happening.
Edit: changed @PostMapping() and @GetMapping() to @PostMapping and @GetMapping
GameStoreApplication.java:
package com.game_store.si2; @SpringBootApplication @ComponentScan("com.game_store.si2.repository") public class GameStoreApplication { public static void main(String[] args) { SpringApplication.run(GameStoreApplication.class, args); } } GameController.java:
package com.game_store.si2.controller; @RestController @RequestMapping("/game") public class GameController { @Autowired private GameRepository repository; @PostMapping public ResponseEntity<Game> newGame(@RequestBody Game novoGame) { if(novoGame.getTitulo() != null) { repository.save(novoGame); return new ResponseEntity<>(HttpStatus.CREATED); } else { return new ResponseEntity<Game>(novoGame, HttpStatus.UNPROCESSABLE_ENTITY); } } @GetMapping("/{id}") public ResponseEntity<Game> findGame(@PathVariable int id){ Optional<Game> gameObtido = repository.findById(id); if(!gameObtido.isPresent()) { return new ResponseEntity<Game>(HttpStatus.OK); } return new ResponseEntity<Game>(HttpStatus.NOT_FOUND); } @GetMapping public Iterable<Game> listGames(){ return repository.findAll(); } @DeleteMapping("/{id}") public ResponseEntity<Game> deleteGame(@PathVariable int id){ Game gameObtido = repository.findById(id).orElse(null); if(gameObtido != null) { repository.delete(gameObtido); return new ResponseEntity<Game>(gameObtido, HttpStatus.NO_CONTENT); } return new ResponseEntity<Game>(HttpStatus.NOT_FOUND); } @PutMapping("/update/{id}") public ResponseEntity<Game> updateGame(@PathVariable int id,@RequestBody Game game){ Game existeGame = repository.findById(id).orElse(null); if(existeGame != null) { existeGame.setImgUrl(game.getImgUrl()); existeGame.setPreco(game.getPreco()); existeGame.setTitulo(game.getTitulo()); repository.save(existeGame); return new ResponseEntity<Game>(existeGame, HttpStatus.OK); } return new ResponseEntity<Game>(HttpStatus.NOT_FOUND); } } Game.java:
package com.game_store.si2.model; @Entity @Getter @Setter @NoArgsConstructor public class Game { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private int id; private String titulo; private String ImgUrl; private double preco; private int numVendas; } GameRepository.java:
package com.game_store.si2.repository; @RepositoryRestResource(collectionResourceRel = "game", path = "game") public interface GameRepository extends CrudRepository<Game, Integer> { } pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.game_store</groupId> <artifactId>game_store</artifactId> <version>0.0.1-SNAPSHOT</version> <name>game_store</name> <description>Projeto de game store de SI2</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> GET and POST return:
{ "timestamp": "2020-09-10T03:41:47.478+00:00", "status": 404, "error": "Not Found", "message": "", "path": "/game" }