5

Helllo. I am trying to use Spring webclient to send post requests and fill the body with an object. But I receive the following " 'fromObject(T)' is deprecate". What is the alternative then ?

WebClient.post()
            .uri("example.com")
            .body(BodyInserters.fromObject(newObject);
Hosten
  • 138
  • 1
  • 11

2 Answers2

5

The javadoc clearly says to use fromValue.

public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromValue(T body)

Inserter to write the given value. Alternatively, consider using the bodyValue(Object) shortcuts on WebClient and ServerResponse.

Type Parameters:

T - the type of the body

Parameters: body - the value to write

Returns:

the inserter to write a single value

Throws:

IllegalArgumentException - if body is a Publisher or an instance of a type supported by ReactiveAdapterRegistry.getSharedInstance(), for which fromPublisher(Publisher, Class) or fromProducer(Object, Class) should be used.

Nagaraju Chitimilla
  • 530
  • 3
  • 7
  • 23
  • Thank you So much. Changing to WebClient.post() .uri("example.com") .body(.fromValue( object); did the trick – Hosten Aug 30 '21 at 09:31
0

I use this way, and it worked

ServerResponse.ok().contentType(MediaType.TEXT_PLAIN) .body(BodyInserters.fromPublisher(Mono.just("Hello World!"),String.class));