如何在Postman或浏览器中测试接收对象参数的POST请求API
测试接收JSON请求体的POST接口时遇到的问题
问题说明
我正在用Postman和浏览器测试Rest API,不清楚如何正确测试接收对象作为参数的POST方法。代码本身没问题(属于Hyperskill项目,已通过所有测试),但测试环节出现异常。
预期测试场景
- 首次发送请求体:
{ "row": 3, "column": 4 }
响应体应返回:
{ "row": 3, "column": 4 }
- 重复发送相同请求体时,响应体返回错误:
{ "error": "The ticket has been already purchased!" }
实际测试异常
- 用浏览器访问
http://localhost:28852/purchase?row=3&column=4,得到405 Method Not Allowed的白标错误页; - Postman测试也出现异常。
Java代码实现
Main类
@SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } }
SeatController类
@RestController public class SeatController { public SeatController() {} private ScreenRoom screenRoom = new ScreenRoom(9, 9); @GetMapping("/seats") public ScreenRoom getSeat(){ return screenRoom; } @PostMapping("/purchase") public synchronized ResponseEntity<?> postSeat(@RequestBody Seat seat){ if (seat.getRow() < 1 || seat.getRow() > 9 || seat.getColumn() < 1 || seat.getColumn() > 9) { return new ResponseEntity<>(Map.of("error", "The number of a row or a column is out of bounds!"), HttpStatus.BAD_REQUEST); } if (screenRoom.getAvailable_seats().contains(seat)) { screenRoom.removeFromAvailableSeats(seat); return new ResponseEntity<>(seat, HttpStatus.OK); } else { return new ResponseEntity<>(Map.of("error", "The ticket has been already purchased!"), HttpStatus.BAD_REQUEST); } } }
ScreenRoom类
public class ScreenRoom { private int total_rows; private int total_columns; private List<Seat> available_seats = new ArrayList<>(); public ScreenRoom(int total_rows, int total_columns) { this.total_rows = total_rows; this.total_columns = total_columns; for (int i = 1; i <= total_rows; i++) { for (int j = 1; j <= total_columns; j++) { available_seats.add(new Seat(i, j)); } } } public int getTotal_rows() { return total_rows; } public void setTotal_rows(int total_rows) { this.total_rows = total_rows; } public int getTotal_columns() { return total_columns; } public void setTotal_columns(int total_columns) { this.total_columns = total_columns; } public List<Seat> getAvailable_seats() { return available_seats; } public void setAvailable_seats(List<Seat> available_seats) { this.available_seats = available_seats; } public void removeFromAvailableSeats(Seat seat) { this.available_seats.remove(seat); } public void getSeat(Seat seat) { this.available_seats.get(this.available_seats.indexOf(seat)); } }
Seat类
public class Seat { private int row; private int column; public Seat(int row, int column) { this.row = row; this.column = column; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getColumn() { return column; } public void setColumn(int column) { this.column = column; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Seat seat = (Seat) o; if (row != seat.row) return false; return column == seat.column; } }
疑问
请问我在测试过程中具体哪里出错了?感谢解答。
内容的提问来源于stack exchange,提问作者Michal
相关产品推荐
相关产品推荐

