Home  >  Forum  >  Spring
Post New Query

Spring WebFlux : No serializer found for class DefaultServerResponseBuilder$BodyInserterResponse



Asked on September 20, 2020
Hi, I am creating Spring WebFlux application using annotation based controller.

@PutMapping("/update")
public Mono<ServerResponse> updateBook(@RequestBody Book book) {
return ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(new Book(102, "dd")));
}

It throws error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.function.server.DefaultServerResponseBuilder$BodyInserterResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1277) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1513) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1005) ~[jackson-databind-2.11.1.jar:2.11.1]
at org.springframework.http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:181) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.http.codec.ServerSentEventHttpMessageWriter.encodeEvent(ServerSentEventHttpMessageWriter.java:172) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.http.codec.ServerSentEventHttpMessageWriter.lambda$encode$0(ServerSentEventHttpMessageWriter.java:156) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) ~[reactor-core-3.3.8.RELEASE.jar:3.3.8.RELEASE]
at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) ~[reactor-core-3.3.8.RELEASE.jar:3.3.8.RELEASE]
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162) ~[reactor-core-3.3.8.RELEASE.jar:3.3.8.RELEASE]
at org.springframework.http.server.reactive.ChannelSendOperator$WriteBarrier.onSubscribe(ChannelSendOperator.java:166) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]





Replied on September 21, 2020
ServerResponse is used in functional web framework. In annotation based controller, you should use ResponseEntity.

You can write your code as following.

@PutMapping("/update")
public Mono<ResponseEntity<Book>> updateBook(@RequestBody Book book) {
return Mono.just(new Book(102, "dd"))
.map(bk -> new ResponseEntity<Book>(bk, HttpStatus.OK));
}




Write Answer








©2020 concretepage.com | Privacy Policy | Contact Us