Question regarding RequestBody and Post

Hi, I’m not sure how to ask this correctly but I’ll try. What parameters to Post in postman if @RequestBody is object (Seat)?
Here is my Java code:

@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(!screenRoom.getAvailable_seats().contains(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<>();
	private ConcurrentMap<Integer, Integer> purchasedTickets = new ConcurrentHashMap<>();
	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;
	private int price;
	public Seat(int row, int column) {
		this.row = row;
		this.column = column;
		this.price = this.row <= 4 ? 10 : 8;
	}
	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;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@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;
    }
}

I tried in Body → raw:

{
    "row": 3,
    "column": 4
}

Or simply following request: “http://localhost:28852/purchase?row=3&column=4”
I know that as Response body I should get:

{
    "row": 3,
    "column": 4,
    "price": 10
}
Instead  I'm getting:
{
    "timestamp": "2023-01-21T14:17:54.425+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/purchase"
}
I know my code works. It's just I dont know how to test in Postman. Hopefully I made myself clear. Thank you for your time and answers.

It would appear on face value that there is something wrong with the request.

The URL looks like its formatted ok, but I have no idea if its correct for the API you’ve built.

I can’t see the word purchase in your code anywhere.

How about if you try and access the URL using a browser instead of Postman. If it produces the same error in the browser, then its not a Postman issue.

http://localhost:28852/purchase?row=3&column=4

Does that error?

There is “purchase” in SeatCOntroller Class. I know there is nothing wrong with Postman. It’s just I dont know how to test my code in Postman or in browser. This url http://localhost:28852/purchase?row=3&column=4. gives me same error as I get in Postman. As I said it is the way I test that’s wrong not the code or Postman.

@PostMapping("/purchase")
	public synchronized ResponseEntity<?> postSeat(@RequestBody Seat seat){
//    		if(!screenRoom.getAvailable_seats().contains(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);
		}
	}

I must admit to being a bit confused.

If you’ve developed the API, then surely you must know what parameters it supports and what you are expecting.

What body it will accept?
Whether its raw, or form data?
What headers are needed?

Pretty sure no one here can tell you that unless you are lucky enough to find a Java developer. (Sounds like this question would be better on a Java forum).

When you design the API, don’t you detail these options before hand, and then build the API to that spec? Rather than build the API and then try to work out what parameters need to be sent?

I’m a Tester, not a developer, but the first thing I would be doing is asking the developer for documentation and a working example.

UPDATE: I was given solution on stackoverflow. It turned out there is No argument constructor missing in Seat class. Then it works.

1 Like