saij9999
saij9999

Reputation: 292

spring web client post form data of Long type

i need to post data to a rest api through form data. the values are long values. i have written the below code, its giving the error.

MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
  formData.add("commentId", Long.valueOf(98578976));
  formData.add("reactionID", Long.valueOf(609878777));


         webClient
          .post()
          .uri(url)
          .contentType(MediaType.APPLICATION_FORM_URLENCODED)
          .header("authorization", accessToken)
          .syncBody(formData)
          .retrieve()
          .bodyToMono(SocialFeed.class)
          .block();

Error:java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.String (java.lang.Long and java.lang.String are in module java.base of loader 'bootstrap').

generally its excepting multi value map of string, string . how to post other types of data ? key is String, value is long ?

Upvotes: 0

Views: 2330

Answers (1)

Alexzander Zharkov
Alexzander Zharkov

Reputation: 181

You need to put all data in the String format for a MultiValueMap.

MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("commentId", String.valueOf(98578976L));
formData.add("reactionID", String.valueOf(609878777L));

The webclient that you use might have issues with casting data to a String type (I believe that what he does under the hood).

What is good for you - that you are able to control what exactly will be posted by casting your data to String manually. So you do not have to rely on internal serializers/casts/etc.

Upvotes: 2

Related Questions