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

huangapple go评论97阅读模式
英文:

Spring Boot GET and POST returning Not Found (CrudReposirory)

问题

基本上就是标题所说的。控制台中没有显示错误,所以我不知道问题出在哪里。我正在使用 CrudRepository。任务是为游戏商店创建一个 REST API。我试图访问 localhost:8080/game 的 URL,但是一直出现 404 错误。

编辑:将 @PostMapping() 和 @GetMapping() 更改为 @PostMapping 和 @GetMapping

GameStoreApplication.java:

  1. package com.game_store.si2;
  2. @SpringBootApplication
  3. @ComponentScan("com.game_store.si2.repository")
  4. public class GameStoreApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(GameStoreApplication.class, args);
  7. }
  8. }

GameController.java:

  1. package com.game_store.si2.controller;
  2. @RestController
  3. @RequestMapping("/game")
  4. public class GameController {
  5. @Autowired
  6. private GameRepository repository;
  7. @PostMapping
  8. public ResponseEntity<Game> newGame(@RequestBody Game novoGame) {
  9. if(novoGame.getTitulo() != null) {
  10. repository.save(novoGame);
  11. return new ResponseEntity<>(HttpStatus.CREATED);
  12. }
  13. else {
  14. return new ResponseEntity<Game>(novoGame, HttpStatus.UNPROCESSABLE_ENTITY);
  15. }
  16. }
  17. @GetMapping("/{id}")
  18. public ResponseEntity<Game> findGame(@PathVariable int id){
  19. Optional<Game> gameObtido = repository.findById(id);
  20. if(!gameObtido.isPresent()) {
  21. return new ResponseEntity<Game>(HttpStatus.OK);
  22. }
  23. return new ResponseEntity<Game>(HttpStatus.NOT_FOUND);
  24. }
  25. @GetMapping
  26. public Iterable<Game> listGames(){
  27. return repository.findAll();
  28. }
  29. @DeleteMapping("/{id}")
  30. public ResponseEntity<Game> deleteGame(@PathVariable int id){
  31. Game gameObtido = repository.findById(id).orElse(null);
  32. if(gameObtido != null) {
  33. repository.delete(gameObtido);
  34. return new ResponseEntity<Game>(gameObtido, HttpStatus.NO_CONTENT);
  35. }
  36. return new ResponseEntity<Game>(HttpStatus.NOT_FOUND);
  37. }
  38. @PutMapping("/update/{id}")
  39. public ResponseEntity<Game> updateGame(@PathVariable int id, @RequestBody Game game){
  40. Game existeGame = repository.findById(id).orElse(null);
  41. if(existeGame != null) {
  42. existeGame.setImgUrl(game.getImgUrl());
  43. existeGame.setPreco(game.getPreco());
  44. existeGame.setTitulo(game.getTitulo());
  45. repository.save(existeGame);
  46. return new ResponseEntity<Game>(existeGame, HttpStatus.OK);
  47. }
  48. return new ResponseEntity<Game>(HttpStatus.NOT_FOUND);
  49. }
  50. }

Game.java:

  1. package com.game_store.si2.model;
  2. @Entity
  3. @Getter
  4. @Setter
  5. @NoArgsConstructor
  6. public class Game {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.SEQUENCE)
  9. private int id;
  10. private String titulo;
  11. private String ImgUrl;
  12. private double preco;
  13. private int numVendas;
  14. }

GameRepository.java:

  1. package com.game_store.si2.repository;
  2. @RepositoryRestResource(collectionResourceRel = "game", path = "game")
  3. public interface GameRepository extends CrudRepository<Game, Integer> {
  4. }

pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.3.3.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.game_store</groupId>
  13. <artifactId>game_store</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>game_store</name>
  16. <description>Projeto de game store de SI2</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-rest</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.h2database</groupId>
  31. <artifactId>h2</artifactId>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <optional>true</optional>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. <exclusions>
  44. <exclusion>
  45. <groupId>org.junit.vintage</groupId>
  46. <artifactId>junit-vintage-engine</artifactId>
  47. </exclusion>
  48. </exclusions>
  49. </dependency>
  50. <dependency>
  51. <groupId>javax.persistence</groupId>
  52. <artifactId>javax.persistence-api</artifactId>
  53. <version>2.2</version>
  54. </dependency>
  55. </dependencies>
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. </plugin>
  62. </plugins>
  63. </build>
  64. </project>

GET 和 POST 返回:

  1. {
  2. "timestamp": "2020-09-10T03:41:47.478+00:00",
  3. "status": 404,
  4. "error": "Not Found",
  5. "message": "",
  6. "path": "/game"
  7. }
英文:

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:

  1. package com.game_store.si2;
  2. @SpringBootApplication
  3. @ComponentScan(&quot;com.game_store.si2.repository&quot;)
  4. public class GameStoreApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(GameStoreApplication.class, args);
  7. }
  8. }

GameController.java:

  1. package com.game_store.si2.controller;
  2. @RestController @RequestMapping(&quot;/game&quot;)
  3. public class GameController {
  4. @Autowired
  5. private GameRepository repository;
  6. @PostMapping
  7. public ResponseEntity&lt;Game&gt; newGame(@RequestBody Game novoGame) {
  8. if(novoGame.getTitulo() != null) {
  9. repository.save(novoGame);
  10. return new ResponseEntity&lt;&gt;(HttpStatus.CREATED);
  11. }
  12. else {
  13. return new ResponseEntity&lt;Game&gt;(novoGame, HttpStatus.UNPROCESSABLE_ENTITY);
  14. }
  15. }
  16. @GetMapping(&quot;/{id}&quot;)
  17. public ResponseEntity&lt;Game&gt; findGame(@PathVariable int id){
  18. Optional&lt;Game&gt; gameObtido = repository.findById(id);
  19. if(!gameObtido.isPresent()) {
  20. return new ResponseEntity&lt;Game&gt;(HttpStatus.OK);
  21. }
  22. return new ResponseEntity&lt;Game&gt;(HttpStatus.NOT_FOUND);
  23. }
  24. @GetMapping
  25. public Iterable&lt;Game&gt; listGames(){
  26. return repository.findAll();
  27. }
  28. @DeleteMapping(&quot;/{id}&quot;)
  29. public ResponseEntity&lt;Game&gt; deleteGame(@PathVariable int id){
  30. Game gameObtido = repository.findById(id).orElse(null);
  31. if(gameObtido != null) {
  32. repository.delete(gameObtido);
  33. return new ResponseEntity&lt;Game&gt;(gameObtido, HttpStatus.NO_CONTENT);
  34. }
  35. return new ResponseEntity&lt;Game&gt;(HttpStatus.NOT_FOUND);
  36. }
  37. @PutMapping(&quot;/update/{id}&quot;)
  38. public ResponseEntity&lt;Game&gt; updateGame(@PathVariable int id,@RequestBody Game game){
  39. Game existeGame = repository.findById(id).orElse(null);
  40. if(existeGame != null) {
  41. existeGame.setImgUrl(game.getImgUrl());
  42. existeGame.setPreco(game.getPreco());
  43. existeGame.setTitulo(game.getTitulo());
  44. repository.save(existeGame);
  45. return new ResponseEntity&lt;Game&gt;(existeGame, HttpStatus.OK);
  46. }
  47. return new ResponseEntity&lt;Game&gt;(HttpStatus.NOT_FOUND);
  48. }
  49. }

Game.java:

  1. package com.game_store.si2.model;
  2. @Entity @Getter @Setter @NoArgsConstructor
  3. public class Game {
  4. @Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  5. private int id;
  6. private String titulo;
  7. private String ImgUrl;
  8. private double preco;
  9. private int numVendas;
  10. }

GameRepository.java:

  1. package com.game_store.si2.repository;
  2. @RepositoryRestResource(collectionResourceRel = &quot;game&quot;, path = &quot;game&quot;)
  3. public interface GameRepository extends CrudRepository&lt;Game, Integer&gt; {
  4. }

pom.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;parent&gt;
  6. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  7. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  8. &lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
  9. &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  10. &lt;/parent&gt;
  11. &lt;groupId&gt;com.game_store&lt;/groupId&gt;
  12. &lt;artifactId&gt;game_store&lt;/artifactId&gt;
  13. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  14. &lt;name&gt;game_store&lt;/name&gt;
  15. &lt;description&gt;Projeto de game store de SI2&lt;/description&gt;
  16. &lt;properties&gt;
  17. &lt;java.version&gt;1.8&lt;/java.version&gt;
  18. &lt;/properties&gt;
  19. &lt;dependencies&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  26. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  27. &lt;/dependency&gt;
  28. &lt;dependency&gt;
  29. &lt;groupId&gt;com.h2database&lt;/groupId&gt;
  30. &lt;artifactId&gt;h2&lt;/artifactId&gt;
  31. &lt;scope&gt;runtime&lt;/scope&gt;
  32. &lt;/dependency&gt;
  33. &lt;dependency&gt;
  34. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  35. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  36. &lt;optional&gt;true&lt;/optional&gt;
  37. &lt;/dependency&gt;
  38. &lt;dependency&gt;
  39. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  40. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  41. &lt;scope&gt;test&lt;/scope&gt;
  42. &lt;exclusions&gt;
  43. &lt;exclusion&gt;
  44. &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
  45. &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
  46. &lt;/exclusion&gt;
  47. &lt;/exclusions&gt;
  48. &lt;/dependency&gt;
  49. &lt;dependency&gt;
  50. &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
  51. &lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
  52. &lt;version&gt;2.2&lt;/version&gt;
  53. &lt;/dependency&gt;
  54. &lt;/dependencies&gt;
  55. &lt;build&gt;
  56. &lt;plugins&gt;
  57. &lt;plugin&gt;
  58. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  59. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  60. &lt;/plugin&gt;
  61. &lt;/plugins&gt;
  62. &lt;/build&gt;
  63. &lt;/project&gt;

GET and POST return:

  1. {
  2. &quot;timestamp&quot;: &quot;2020-09-10T03:41:47.478+00:00&quot;,
  3. &quot;status&quot;: 404,
  4. &quot;error&quot;: &quot;Not Found&quot;,
  5. &quot;message&quot;: &quot;&quot;,
  6. &quot;path&quot;: &quot;/game&quot;
  7. }

答案1

得分: 1

你可以将下面的方法中的 @GetMapping() 替换为 @GetMapping,将 @PostMapping() 替换为 @PostMapping,就像这样:

  1. @GetMapping
  2. public Iterable<Game> listGames(){
  3. return repository.findAll();
  4. }
  5. @PostMapping
  6. public ResponseEntity<Game> newGame(@RequestBody Game novoGame) {
  7. if(novoGame.getTitulo() != null) {
  8. repository.save(novoGame);
  9. return new ResponseEntity<>(HttpStatus.CREATED);
  10. }
  11. else {
  12. return new ResponseEntity<Game>(novoGame, HttpStatus.UNPROCESSABLE_ENTITY);
  13. }
  14. }
英文:
  1. You can replace @GetMapping() to @GetMapping and @PostMapping() to @PostMapping
  2. in below method respectively like
  3. @GetMapping
  4. public Iterable&lt;Game&gt; listGames(){
  5. return repository.findAll();
  6. }
  7. @PostMapping
  8. public ResponseEntity&lt;Game&gt; newGame(@RequestBody Game novoGame) {
  9. if(novoGame.getTitulo() != null) {
  10. repository.save(novoGame);
  11. return new ResponseEntity&lt;&gt;(HttpStatus.CREATED);
  12. }
  13. else {
  14. return new ResponseEntity&lt;Game&gt;(novoGame, HttpStatus.UNPROCESSABLE_ENTITY);
  15. }
  16. }

答案2

得分: 0

使用 GetMappingPostMapping 的默认值,您可以使用以下方式:

  1. @GetMapping
  2. @PostMapping

或者

  1. @GetMapping("")
  2. @PostMapping("")
英文:

To use default value of GetMapping and PostMapping you can use

  1. @GetMapping
  2. @PostMapping

or

  1. @GetMapping(&quot;&quot;)
  2. @PostMapping(&quot;&quot;)

答案3

得分: 0

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

英文:

just rename @PostMapping() to @PostMapping

huangapple
  • 本文由 发表于 2020年9月10日 11:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63822555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定