[英]Unsupported media type on postman POST call for SpringBoot REST API in terminal
我已經構建了一個基本的 SpringBoot REST API,當我嘗試使用 POSTMAN 添加玩家時,我在 CMD 中收到以下錯誤:
curl -X POST "Content-Type: application/json" http://localhost:8080/players -d '{"firstName": "Magnus", "lastName": "Carlsen", "email": "magnuscarlsen@gmail.com"}'
curl: (3) Port number ended with ' '
{"timestamp":"2021-11-03T16:31:33.626+00:00","status":415,"error":"Unsupported Media Type","path":"/players"}curl: (6) Could not resolve host: Magnus,
curl: (6) Could not resolve host: lastName
curl: (6) Could not resolve host: Carlsen,
curl: (6) Could not resolve host: email
curl: (3) [globbing] unmatched close brace/bracket in column 24
如果我只是這樣做:
curl localhost:8080/players
我得到一個空的 JSON 對象,以便正常工作。
控制器:
@RestController
@RequestMapping("/players")
public class PlayersController {
private final PlayerRepository playerRepository;
public PlayersController(PlayerRepository playerRepository) {
this.playerRepository = playerRepository;
}
@GetMapping
public List<Player> getClients() {
return playerRepository.findAll();
}
@GetMapping("/{id}")
public Player getPlayer(@PathVariable Long id) {
return playerRepository.findById(id).orElseThrow(RuntimeException::new);
}
@PostMapping
public ResponseEntity createPlayer(@RequestBody Player player) throws URISyntaxException {
Player savedPlayer = playerRepository.save(player);
return ResponseEntity.created(new URI("/players/" + savedPlayer.getId())).body(savedPlayer);
}
@PutMapping("/{id}")
public ResponseEntity updatePlayer(@PathVariable Long id, @RequestBody Player player) {
Player currentPlayer = playerRepository.findById(id).orElseThrow(RuntimeException::new);
currentPlayer.setFirstName(player.getFirstName());
currentPlayer.setFirstName(player.getLastName());
currentPlayer.setEmail(player.getEmail());
currentPlayer = playerRepository.save(player);
return ResponseEntity.ok(currentPlayer);
}
@DeleteMapping("/{id}")
public ResponseEntity deletePlayer(@PathVariable Long id) {
playerRepository.deleteById(id);
return ResponseEntity.ok().build();
}
}
我在網上查找了這個被禁止的媒體類型錯誤,但每個解決方案都在使用某種程序,而不僅僅是在終端 CMD 中,因此任何幫助將不勝感激。 謝謝
在我看來,您的 curl 命令不正確。 請嘗試以下操作:
curl -X POST http://localhost:8080/players -H 'Content-Type: application/json' -d '{"firstName": "Magnus", "lastName": "Carlsen", "email": "magnuscarlsen@gmail.com"}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.