简体   繁体   English

Spring Boot GET 和 POST 返回 Not Found (CrudReposirory)

[英]Spring Boot GET and POST returning Not Found (CrudReposirory)

Basically what the title says.基本上就是标题所说的。 No errors show in the console so I have no idea what's wrong.控制台中没有显示错误,所以我不知道出了什么问题。 I am using CrudRepository.我正在使用 CrudRepository。 The task is to make a REST API for a game store.任务是为游戏商店制作 REST API。 I am trying to access localhost:8080/game URL but 404 keeps happening.我正在尝试访问 localhost:8080/game URL,但 404 不断发生。

Edit: changed @PostMapping() and @GetMapping() to @PostMapping and @GetMapping编辑:将@PostMapping() 和@GetMapping() 更改为@PostMapping 和@GetMapping

GameStoreApplication.java:游戏商店应用程序.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:游戏控制器.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:游戏.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: GameRepository.java:

package com.game_store.si2.repository;

@RepositoryRestResource(collectionResourceRel = "game", path = "game")
public interface GameRepository extends CrudRepository<Game, Integer> {

}

pom.xml: 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: GET 和 POST 返回:

{
    "timestamp": "2020-09-10T03:41:47.478+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/game"
}
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);
    }
}

To use default value of GetMapping and PostMapping you can use要使用GetMappingPostMapping默认值,您可以使用

@GetMapping
@PostMapping

or或者

@GetMapping("")
@PostMapping("")

只需将@PostMapping() 重命名为@PostMapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM