org.springframework.core.codec.CodecException: Error while writing the data; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.http.codec.ServerSentEvent and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

Spring JIRA | Jose Antonio Iñigo | 2 months ago
  1. 0

    After upgrading from Spring 5.0.0 M1 to M2 the following code has stopped working (I changed from SseEvent to ServerSentEvent class): {code:java} @RestController public class StringsRestController { @GetMapping("/strings/sse/event") public Flux<ServerSentEvent<String>> sse() { return Flux.interval(Duration.ofMillis(100)).map(l -> { ServerSentEvent<String> event = ServerSentEvent.builder("foo").build(); return event; }).take(2); } } {code} I have uploaded a sample project at this URL (branch spring5-m2): https://github.com/codependent/spring5-playground/tree/spring-5-m2 To reproduce: 1) Start the application with com.codependent.spring5.playground.Spring5PlaygroundApplication 2) Call http://localhost:8080/strings/serversentsevent (com.codependent.spring5.playground.reactive.web.StringsRestController.sse()) This is the exception: {code:java} 2016-09-26 08:50:10.966 DEBUG 10488 --- [nio-8080-exec-1] o.s.web.reactive.DispatcherHandler : Processing GET request for [http://localhost:8080/strings/serversentsevent] 2016-09-26 08:50:10.997 DEBUG 10488 --- [nio-8080-exec-1] s.w.r.r.m.a.RequestMappingHandlerMapping : Looking up handler method for path /strings/serversentsevent 2016-09-26 08:50:11.002 DEBUG 10488 --- [nio-8080-exec-1] s.w.r.r.m.a.RequestMappingHandlerMapping : Returning handler method [public reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<java.lang.String>> com.codependent.spring5.playground.reactive.web.StringsRestController.sse()] 2016-09-26 08:50:11.002 DEBUG 10488 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'stringsRestController' 2016-09-26 08:50:11.407 DEBUG 10488 --- [ timer-1] o.s.w.s.h.ExceptionHandlingWebHandler : Could not complete request org.springframework.core.codec.CodecException: Error while writing the data; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.http.codec.ServerSentEvent and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) at org.springframework.http.codec.json.Jackson2JsonEncoder.encodeValue(Jackson2JsonEncoder.java:134) ~[spring-web-5.0.0.M2.jar:5.0.0.M2] at org.springframework.http.codec.json.Jackson2JsonEncoder.lambda$encode$2(Jackson2JsonEncoder.java:100) ~[spring-web-5.0.0.M2.jar:5.0.0.M2] at org.springframework.http.codec.json.Jackson2JsonEncoder$$Lambda$75.0000000013BE51F0.apply(Unknown Source) ~[na:na] at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:339) [reactor-core-3.0.2.RELEASE.jar:na] at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onNext(FluxConcatMap.java:232) [reactor-core-3.0.2.RELEASE.jar:na] at reactor.core.publisher.FluxTake$TakeSubscriber.onNext(FluxTake.java:136) [reactor-core-3.0.2.RELEASE.jar:na] at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:122) [reactor-core-3.0.2.RELEASE.jar:na] at reactor.core.publisher.FluxInterval$IntervalRunnable.run(FluxInterval.java:93) [reactor-core-3.0.2.RELEASE.jar:na] at reactor.core.scheduler.SingleTimedScheduler$TimedPeriodicScheduledRunnable.run(SingleTimedScheduler.java:383) [reactor-core-3.0.2.RELEASE.jar:na] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522) [na:1.8.0-internal] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:319) [na:1.8.0-internal] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:191) [na:1.8.0-internal] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) [na:1.8.0-internal] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153) [na:1.8.0-internal] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [na:1.8.0-internal] at java.lang.Thread.run(Thread.java:785) [na:1.8.0-internal] Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.http.codec.ServerSentEvent and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:275) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1419) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1147) ~[jackson-databind-2.8.3.jar:2.8.3] at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:977) ~[jackson-databind-2.8.3.jar:2.8.3] at org.springframework.http.codec.json.Jackson2JsonEncoder.encodeValue(Jackson2JsonEncoder.java:131) ~[spring-web-5.0.0.M2.jar:5.0.0.M2] ... 15 common frames omitted 2016-09-26 08:50:11.407 DEBUG 10488 --- [ timer-1] o.s.h.s.r.ServletServerHttpResponse : Can't set the status 500 because the HTTP response has already been committed 2016-09-26 08:50:11.407 DEBUG 10488 --- [ timer-1] o.s.h.s.r.ServletHttpHandlerAdapter : Successfully completed request {code}

    Spring JIRA | 2 months ago | Jose Antonio Iñigo
    org.springframework.core.codec.CodecException: Error while writing the data; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.http.codec.ServerSentEvent and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

    Root Cause Analysis

    1. org.springframework.core.codec.CodecException

      Error while writing the data; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.http.codec.ServerSentEvent and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

      at org.springframework.http.codec.json.Jackson2JsonEncoder.encodeValue()
    2. org.springframework.http
      Jackson2JsonEncoder.lambda$encode$2
      1. org.springframework.http.codec.json.Jackson2JsonEncoder.encodeValue(Jackson2JsonEncoder.java:134)[spring-web-5.0.0.M2.jar:5.0.0.M2]
      2. org.springframework.http.codec.json.Jackson2JsonEncoder.lambda$encode$2(Jackson2JsonEncoder.java:100)[spring-web-5.0.0.M2.jar:5.0.0.M2]
      2 frames