java 如何通过在Postman中传递对象或在浏览器中传递URL来发布Post请求

67up9zun  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(234)

我正在Postman和浏览器中的URL上测试我的Rest API,但是我不知道如何用POST方法测试一个API,POST方法把一个对象作为参数。我知道我的代码没有问题,因为它是Hyperskill项目的一部分,并且通过了所有的测试。只是我不知道如何测试它,我在任何地方都找不到答案。
下面是我应该测试的内容和应该得到的结果:

  • 请求正文:*
{
    "row": 3,
    "column": 4
}
  • 回复正文:*
{
    "row": 3,
    "column": 4,
}

当我再次尝试相同的 * 请求正文 * 时:

{
    "row": 3,
    "column": 4
}
  • 响应正文 * 应该给予我一个错误消息:
{
    "error": "The ticket has been already purchased!"
}

相反,当我在浏览器中测试以下URL http://localhost:28852/purchase?row = 3&column = 4时,我得到:
Whitelabel Error Page此应用程序没有与/error的显式Map,因此您将此视为一个回退。Mon Jan 23 08:53:33 CET 2023出现了一个意外错误(类型=方法不允许,状态=405)。
当我尝试在Postman中测试我的代码时,我得到:

这是我用Java编写的完整代码:

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}
@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);
        }
    }
}
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));
    }
}
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;
    }
}

关于测试,我到底做错了什么?谢谢你的时间和答案。

lmyy7pcs

lmyy7pcs1#

你不能在浏览器上进行POST,除非通过js fetch。你只能使用GET。“Method Not Allowed,status=405 error”(方法不被允许,状态=405错误)就是在告诉你,即GET不被允许。
你从 Postman 那里得到的错误显示你已经通过了405(因为这是一个POST),但是服务器正在抛出异常。你需要检查日志或者在调试中单步执行。

相关问题