I'm have issues with passing JSON to RestController. It doesn't seem to consume it.
Controller:
@PostMapping(path = "Users/{UserId}/Transactions",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public CompletableFuture<ResponseEntity<?>> startGameRound(@RequestBody TransactionRequest request,
@RequestParam("PartnerUserSessionKey") String sessionId,
@PathVariable("UserId") String playerUUID) throws ExecutionException, InterruptedException { // Logic }
TransactionRequest
Object Model:
public class TransactionRequest {
@JsonProperty("TransactionType")
private TransactionType transactionType;
@JsonProperty("TransactionId")
private String transactionId;
@JsonProperty("TransactionCreationDate")
private LocalDateTime transactionCreationDate;
@JsonProperty("Amount")
private Long amount;
@JsonProperty("Rake")
private BigDecimal rake;
@JsonProperty("CurrencyCode")
private String currencyCode;
@JsonProperty("EntityReferences")
private List<EntityReference> entityReferences;
@JsonProperty("Game")
private Game game;
public TransactionRequest() {
}
//getters & setters & hashEquals & toString
}
And here is the Test Method that's trying to Post to the Controller:
def createTransactionRequest(Integer roundNum, String transType, BigDecimal transactionAmount) {
def transactionRequest = builder{
'TransactionType' transType
'TransactionId' "${new Random().nextInt(50)}"
'TransactionCreationDate' LocalDateTime.now().toString()
'Amount' transactionAmount.longValue()
'Rake' 0.0
'CurrencyCode' userCurrencyCode
'EntityReferences' builder.call([
{ 'EntityType' "CasinoRound"; 'EntityId' roundNum },
{ 'EntityType' "CasinoSession"; 'EntityId' gameSessionId }
])
'Game' (GameId: "111", GameName: "Some Game")
}
String currentDate = ZonedDateTime.now(ZoneId.of("UTC")).format(DATE_FORMATTER)
def authorizationHeader = buildAuthHeader("POST",
"Users/${player.uuid}/Transactions?PartnerUserSessionKey=$gameSessionId",
JsonOutput.toJson(transactionRequest), currentDate)
return client.post(path: "Users/${player.uuid}/Transactions", query: ["PartnerUserSessionKey": gameSessionId],
headers: ['Authorization' : authorizationHeader, DateUtc : currentDate]) {
type "application/json"
json transactionRequest
}
}
Test method is written in Groovy. And Builder as in groovy.json.JsonBuilder
. (Ignore authorizationHeader
method it's just for generating Signature for Authorization, it works) I've tried just Sending several parameters only but it would give me same Error every time. Here is the Error:
2017-09-05 12:38:26,005 WARN c.n.c.e.CommonExceptionHandler - Required request body is missing: public java.util.concurrent.CompletableFuture<org.springframework.http.ResponseEntity<?>> com.project.TransactionEndpoint.startGameRound(com.project.api.TransactionRequest,java.lang.String,java.lang.String) throws java.util.concurrent.ExecutionException,java.lang.InterruptedException
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.util.concurrent.CompletableFuture<org.springframework.http.ResponseEntity<?>> com.project.endpoint.TransactionEndpoint.startGameRound(com.project.api.TransactionRequest,java.lang.String,java.lang.String) throws java.util.concurrent.ExecutionException,java.lang.InterruptedException
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:153)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:127)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
I believe it has something to do Object Model but not sure what. I've tried passing different combinations but no luck. It would just give me Required request body is missing
.
Here is the resulting JSON:
{"TransactionType":"CasinoRound_Stake","TransactionId":"31","TransactionCreationDate":"2017-09-05T15:38:08.610","Amount":400,"Rake":0.0,"CurrencyCode":"EUR","EntityReferences":[{"EntityType":"CasinoRound","EntityId":1},{"EntityType":"CasinoSession","EntityId":"9f31d8b9-28f7-4931-bb9d-73f90c2b2de7"}],"Game":{"GameId":"111","GameName":"Some Game"}}
You probably have a filter in which you have already read the body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With