有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring:缺少必需的请求正文。无法将JSON传递给RestController

我在将JSON传递给RestController时遇到问题。它似乎没有消耗它

控制器:

@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对象模型:

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 
}

以下是试图发布到控制器的测试方法:

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
    }

}

测试方法是用Groovy编写的。和构建器,如groovy.json.JsonBuilder。(忽略authorizationHeader方法这只是为了生成授权签名,它可以工作)我尝试只发送几个参数,但每次都会给我相同的错误。以下是错误:

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)

我相信它与对象模型有关,但不确定是什么。我试过通过不同的组合,但没有运气。它只会给我Required request body is missing

以下是生成的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"}}

共 (1) 个答案

  1. # 1 楼答案

    你可能有一个过滤器,在其中你已经阅读了正文