I’m learning spring boot with Kotlin and I’m trying to do a WebClient call with Flux. I have these m...
d

dany giguere

over 2 years ago
I’m learning spring boot with Kotlin and I’m trying to do a WebClient call with Flux. I have these methods in a controller:
@GetMapping("/slow-service-users")
    fun getAllUsers(): List<User?>? {
        Thread.sleep(2000L) // delay
        return userRepository.findAll()
    }

    @GetMapping(value = ["/users-non-blocking"])
    fun getUsersNonBlocking(): Flux<User?>? {
        <http://logger.info|logger.info>("Starting NON-BLOCKING Controller!")
        val userFlux: Flux<User?> = WebClient.create()
            .get()
            .uri("<http://localhost:8080/slow-service-users>")
            .retrieve()
            .bodyToFlux(User::class.java)
        userFlux.subscribe { user -> <http://logger.info|logger.info>(user.toString()) }
        <http://logger.info|logger.info>("Exiting NON-BLOCKING Controller!")
        return userFlux
    }
/slow-service-users
returns a proper response. But when I call
/users-non-blocking
, I get this error:
"org.springframework.web.reactive.function.client.WebClientResponseException: 200 OK from GET <http://localhost:8080/slow-service-users>; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=com.example.app.models.User
How can I fix this
Content type 'application/json' not supported for bodyType
error ? The project repo is here: https://github.com/danygiguere/springboot-kotlin-example. The user model is:
@Entity
class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long? = null

    var username = ""

    @Column(unique = true)
    var email = ""

    var password = ""
        @JsonIgnore
        get() = field
        set(value) {
            val passwordEncoder = BCryptPasswordEncoder()
            field = passwordEncoder.encode(value)
        }

    fun comparePassword(password: String): Boolean {
        return BCryptPasswordEncoder().matches(password, this.password)
    }

    val firstName: String = ""

    val lastName: String = ""

    @JsonManagedReference
    @OneToMany(cascade = [(CascadeType.ALL)], orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = "userId", referencedColumnName = "id")
    var posts: List<Post>? = null
}