'How to send 2D array to the spring-boot post request from Postman?
I am trying to send a POST request to spring-boot controller from the postman. The postman body will be a 2d array. I want to catch the 2d array in @requestBody and work with that array.
Code:
Postman schema: url : http://localhost:8081/api/ticTacToe/gameWinner
body : {
"moves" : [["CROSS","CIRCLE","CROSS"],["CROSS","CROSS","CROSS"],["CROSS","CIRCLE","CROSS"],
["CROSS","CIRCLE","CROSS"],["CROSS","CIRCLE","CROSS"],["CROSS","CIRCLE","CROSS"],
["CROSS","CIRCLE","CROSS"],["CROSS","CIRCLE","CROSS"],["CROSS","CIRCLE","CROSS"]]
}
Controller :
package com.example.TicTacToe.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.TicTacToe.Service.TicTacToeService;
@RestController
@RequestMapping("/api/ticTacToe")
public class TicTacToeController {
private static final Logger LOGGER = LoggerFactory.getLogger(TicTacToeController.class);
@Autowired
TicTacToeService gameService;
@PostMapping("/gameWinner")
public ResponseEntity<String> resultOfTheGame(@RequestBody String[][]moves){
try {
String winner = "";
winner = gameService.winnerOfTheGame(moves);
if(winner.equals("CROSS")){
System.out.println("CROSS wins!");
}else if(winner.equals("CIRCLE")){
System.out.println("CIRCLE wins!");
}else{
System.out.println("Draw!");
}
}catch(Exception e) {
LOGGER.error("Exception Thrown {}", e.getLocalizedMessage());
return new ResponseEntity<>(e.getLocalizedMessage(),HttpStatus.EXPECTATION_FAILED);
}
return null;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
