英文:
Spring Boot GET and POST returning Not Found (CrudReposirory)
问题
基本上就是标题所说的。控制台中没有显示错误,所以我不知道问题出在哪里。我正在使用 CrudRepository。任务是为游戏商店创建一个 REST API。我试图访问 localhost:8080/game 的 URL,但是一直出现 404 错误。
编辑:将 @PostMapping() 和 @GetMapping() 更改为 @PostMapping 和 @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 和 POST 返回:
{
"timestamp": "2020-09-10T03:41:47.478+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/game"
}
英文:
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"
}
答案1
得分: 1
你可以将下面的方法中的 @GetMapping() 替换为 @GetMapping,将 @PostMapping() 替换为 @PostMapping,就像这样:
@GetMapping
public Iterable<Game> listGames(){
return repository.findAll();
}
@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);
}
}
英文:
You can replace @GetMapping() to @GetMapping and @PostMapping() to @PostMapping
in below method respectively like
@GetMapping
public Iterable<Game> listGames(){
return repository.findAll();
}
@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);
}
}
答案2
得分: 0
使用 GetMapping
和 PostMapping
的默认值,您可以使用以下方式:
@GetMapping
@PostMapping
或者
@GetMapping("")
@PostMapping("")
英文:
To use default value of GetMapping
and PostMapping
you can use
@GetMapping
@PostMapping
or
@GetMapping("")
@PostMapping("")
答案3
得分: 0
只需将@PostMapping()
重命名为@PostMapping
。
英文:
just rename @PostMapping() to @PostMapping
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论