Unrecognized token 'message': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
Asked Answered
P

3

7

When I send a post request, I get this error.

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]

and this is 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]

I have such a this JSON which sending to the server from client.

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

and this is my service layer method which generate Webclient codes in there.

   @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)));
    }

although I am getting error, but data successfully stored into database.

This is me dto class

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

    @JsonProperty("message")
    private String message;

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

}

and this is my server response code:

@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";
}
Papua answered 31/10, 2021 at 20:31 Comment(4)
Is the save method the function which is sending the content data to the clients (the one under the API/@Controller)? Or is it a business service piece?Oxfordshire
yes it is. this method called into service layer of method , which called insert();Papua
I suspect the error is related to the "data contract" between the client and the server. Could you share the response payload (example the final part of your API controller or trying to enable some logging like on baeldung.com/spring-log-webclient-calls) or doing a manual post curl? The client is expecting some with the form of {"message":"", etc..} but the server is sending back other.Oxfordshire
In my case, I just used Gson instead.Nourish
F
2

It seems to me that you are sending a message property that is not expected at all by the service you are calling.

You are setting BulkSmsRequestResourceTest as both the body request and also the body response. Are you sure that the server is expecting message property in the request and will also include message in the response?


Your additional code just shows this is in fact true. Your server is answering with a simple String ("message success sended"), but you are expecting it to be an object (BulkSmsRequestResourceTest). Please update your code to the following one:

@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)));
}
Faraway answered 31/10, 2021 at 20:38 Comment(4)
I can receive messages on the server side.Papua
Yeah I know, but is the server expecting a message property in your JSON object?Humeral
@AnarMəmmədov I've edited my answer, please check it ;)Humeral
I changed my code, it worked!Papua
O
2

If server got your request and it executed the expected logic, the problem should be related to its Json response and the structure of your BulkSmsRequestResourceTest class.

The error is during deserializing process, so your client has some data to convert. Probably your class or your object mapper settings are not configurated properly.

If you need more info, you should share:

  • Json response
  • BulkSmsRequestResourceTest class
  • ObjectMapper settings or tell if's the default one
Oxfordshire answered 31/10, 2021 at 20:47 Comment(3)
I updated my answer and also I have not ObjectMapper.Papua
Could you add also the server response? If you need a way to log web client response body, you can try this one baeldung.com/spring-log-webclient-callsOxfordshire
I updated again and understood, but What do am I?Papua
L
2

My issues was I needed to JSON.stringify() my data before making the API call in my front end. I guess jquery wasn't doing that automatically

Linhliniment answered 27/3, 2023 at 19:25 Comment(1)
This saved me hours of debugging. Thank youIntegrator

© 2022 - 2025 — McMap. All rights reserved.