Java 未找到Spring Boot GET和POST返回(CRUDEPOSIRORY)
基本上就是标题所说的。控制台中没有显示错误,因此我不知道出了什么问题。我在用积垢沉淀剂。任务是为游戏商店制作一个RESTAPI。我试图访问localhost:8080/游戏URL,但404一直在发生 编辑:将@PostMapping()和@GetMapping()更改为@PostMapping和@GetMapping() GameStoreApplication.java:Java 未找到Spring Boot GET和POST返回(CRUDEPOSIRORY),java,spring-boot,model-view-controller,repository,crud,Java,Spring Boot,Model View Controller,Repository,Crud,基本上就是标题所说的。控制台中没有显示错误,因此我不知道出了什么问题。我在用积垢沉淀剂。任务是为游戏商店制作一个RESTAPI。我试图访问localhost:8080/游戏URL,但404一直在发生 编辑:将@PostMapping()和@GetMapping()更改为@PostMapping和@GetMapping() GameStoreApplication.java: package com.game_store.si2; @SpringBootApplication @Componen
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);
}
}
GameRepository.java:
package com.game_store.si2.repository;
@RepositoryRestResource(collectionResourceRel = "game", path = "game")
public interface GameRepository extends CrudRepository<Game, Integer> {
}
要使用
GetMapping
和PostMapping
的默认值,可以使用
@GetMapping
@PostMapping
@GetMapping("")
@PostMapping("")
或
您可以将@GetMapping()替换为@GetMapping,将@PostMapping()替换为@PostMapping
在下面的方法中分别喜欢
@GetMapping
公共游戏{
返回repository.findAll();
}
@邮戳
公共响应性新游戏(@RequestBody-Game-novoGame){
if(novoGame.getitulo()!=null){
保存(novoGame);
返回新的ResponseEntity(HttpStatus.CREATED);
}
否则{
返回新的ResponseEntity(novoGame、HttpStatus.不可处理的实体);
}
}
只需将@PostMapping()重命名为@PostMapping您能分享准确的卷曲请求吗?我不知道如何获得它。-->这不起作用吗??或者你有另一个端口?我只需要请求url的详细信息。哦,好的,它是localhost:8080/游戏,但我实际上已经修复了它。结果是(我认为)当我重新启动它的整个工作时,是Eclipse造成的。不过还是要谢谢你,伙计。我先做了一个,然后做了另一个,但没有一个解决了这个错误
{
"timestamp": "2020-09-10T03:41:47.478+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/game"
}
@GetMapping
@PostMapping
@GetMapping("")
@PostMapping("")
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);
}
}