提问者:小点点

未找到Spring Boot GET和POST返回(CRUDEPOSIRORY)


基本上就是标题所说的。控制台中没有显示错误,因此我不知道出了什么问题。我用的是积垢沉淀剂。任务是为游戏商店制作一个RESTAPI。我试图访问localhost:8080/游戏URL,但404一直在发生。

编辑:将@PostMapping()和@GetMapping()更改为@postmaping和@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);
    }

}

游戏控制器.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);
    }
    
}

游戏.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>

获取并发布返回:

{
    "timestamp": "2020-09-10T03:41:47.478+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/game"
}

共3个答案

匿名用户

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);
    }
}

匿名用户

要使用< code>GetMapping和< code>PostMapping的默认值,您可以使用

@GetMapping
@PostMapping

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

匿名用户

只需将@PostMapping()重命名为@postmaping即可