BodyInserters.fromObject is deprecated, what is the alternative?
Asked Answered
C

2

6

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);
Cauca answered 30/8, 2021 at 9:12 Comment(0)
M
7

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.

Mensurable answered 30/8, 2021 at 9:20 Comment(1)
Thank you So much. Changing to WebClient.post() .uri("example.com") .body(.fromValue(<T> object); did the trickCauca
T
0

I use this way, and it worked

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

Tradeswoman answered 27/7, 2023 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.