java 无法识别的令牌“message”:(JSON字符串、数字、数组、对象或标记“null”、“true”或“false”)

nwlqm0z1  于 2023-03-28  发布在  Java
关注(0)|答案(3)|浏览(291)

当我发送一个post请求时,我得到这个错误。

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'message': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 9]

这是Stack Trace:

Stack trace:
        at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:242) ~[spring-web-5.3.12.jar:5.3.12]
        at org.springframework.http.codec.json.AbstractJackson2Decoder.decode(AbstractJackson2Decoder.java:198) ~[spring-web-5.3.12.jar:5.3.12]
        at org.springframework.http.codec.json.AbstractJackson2Decoder.lambda$decodeToMono$1(AbstractJackson2Decoder.java:179) ~[spring-web-5.3.12.jar:5.3.12]
        at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:125) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:295) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:337) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:260) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142) ~[reactor-core-3.4.11.jar:3.4.11]
        at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:400) ~[reactor-netty-core-1.0.12.jar:1.0.12]
        at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:419) ~[reactor-netty-core-1.0.12.jar:1.0.12]
        at reactor.netty.channel.ChannelOperations.terminate(ChannelOperations.java:473) ~[reactor-netty-core-1.0.12.jar:1.0.12]
        at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:702) ~[reactor-netty-http-1.0.12.jar:1.0.12]

我有这样一个JSON,从客户端发送到服务器。

{
  "message": "string",
  "originator": "string",
  "receivers": "string"
}

这是我的服务层方法,它在那里生成Webclient代码。

@Override
    public Mono<ResponseEntity<? extends ResponseResource>> sendSms(BulkSmsRequestResourceTest request) {
        if (request == null) {
            return Mono.just(new ResponseEntity<>(
                    new ErrorResponseResource(
                            "Transaction failed unsuccessfully!",
                            400),
                    HttpStatus.BAD_REQUEST));
        }
        Mono<BulkSmsRequestResourceTest> bulkSmsRequestResourceMono = webClientBuilder.build()
                .post()
                .uri(sendSmsService)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(Mono.just(request), BulkSmsRequestResourceTest.class)
                .retrieve()
                .bodyToMono(BulkSmsRequestResourceTest.class);
        bulkSmsRequestResourceMono.subscribe();
        return Mono.just((new ResponseEntity<>(new SuccessResponseResource("Transaction done successfully", 200), HttpStatus.OK)));
    }

虽然我得到错误,但数据成功地存储到数据库。
这是我的dto课

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BulkSmsRequestResourceTest {

    @JsonProperty("message")
    private String message;

    @JsonProperty("receivers")
    private String receivers;
    
    @JsonProperty("originator")
    private String originator;

}

这是我的服务器响应代码:

@Override
public String save(BulkSmsRequestDto bulkSmsRequestDto) {
if (bulkSmsRequestDto == null) {
    return "message unsuccess sended";
}
BulkSms bulkSms = BulkSms.builder()
        .message(bulkSmsRequestDto.getMessage())
        .phoneNumber(bulkSmsRequestDto.getReceivers())
        .originator(bulkSmsRequestDto.getOriginator())
        .build();
this.bulkSmsRepository.save(bulkSms);
return "message success sended";
}
mi7gmzs6

mi7gmzs61#

在我看来,你发送的message属性根本不是你所调用的服务所期望的。
您将BulkSmsRequestResourceTest设置为请求正文和响应正文。您确定服务器在请求中需要message属性,并且在响应中也包含message吗?
您的附加代码只是表明这实际上是正确的。您的服务器使用简单的String(“message success sended”)进行响应,但您希望它是一个对象(BulkSmsRequestResourceTest)。请将您的代码更新为以下代码:

@Override
public Mono<ResponseEntity<? extends ResponseResource>> sendSms(BulkSmsRequestResourceTest request) {
   if (request == null) {
       return Mono.just(new ResponseEntity<>(
               new ErrorResponseResource(
                       "Transaction failed unsuccessfully!",
                       400),
               HttpStatus.BAD_REQUEST));
   }
   Mono<String> bulkSmsRequestResourceMono = webClientBuilder.build()
           .post()
           .uri(sendSmsService)
           .contentType(MediaType.APPLICATION_JSON)
           .accept(MediaType.APPLICATION_JSON)
           .body(Mono.just(request), BulkSmsRequestResourceTest.class)
           .retrieve()
           .bodyToMono(String.class);
   bulkSmsRequestResourceMono.subscribe();
   return Mono.just((new ResponseEntity<>(new SuccessResponseResource("Transaction done successfully", 200), HttpStatus.OK)));
}
q3aa0525

q3aa05252#

如果服务器收到了你的请求,并且执行了预期的逻辑,那么问题应该与它的Json响应和你的BulkSmsRequestResourceTest类的结构有关。
这个错误发生在反序列化过程中,所以你的客户端有一些数据要转换。可能是你的类或对象Map器设置没有正确配置。
如果您需要更多信息,请分享:

  • Json响应
  • BulkSmsRequestResourceTest
  • ObjectMapper设置或告诉是否为默认设置
abithluo

abithluo3#

我的问题是我需要在前端调用API之前对数据进行JSON.stringify(),我猜jquery没有自动完成这个任务

相关问题