dany giguere
09/12/2022, 12:07 PM@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
}
Giorgos Makris
09/12/2022, 7:46 PMMono<ServerResponse>
Giorgos Makris
09/12/2022, 7:46 PMdany giguere
09/12/2022, 8:41 PMMono…
instead of Flux<User?>
?kqr
09/13/2022, 6:34 AMGiorgos Makris
09/13/2022, 7:34 AMFlux<User>
dany giguere
09/15/2022, 8:01 PM@GetMapping(value = ["/users-non-blocking"], produces = ["application/json"])
fun getUsersNonBlocking(): Mono<User> {
<http://logger.info|logger.info>("Starting NON-BLOCKING Controller!")
val userFlux: Mono<User> = WebClient.create()
.get()
.uri("<http://localhost:8080/slow-service-users>")
.header("ACCEPT", "application/json")
.retrieve()
.bodyToMono(User::class.java)
userFlux.subscribe { user -> <http://logger.info|logger.info>(user.toString()) }
<http://logger.info|logger.info>("Exiting NON-BLOCKING Controller!")
return userFlux
}
kqr
09/16/2022, 6:34 AMdany giguere
09/16/2022, 8:11 PMproduces = [“application/json”]
as well on the slow-service tookqr
09/16/2022, 9:11 PMdany giguere
09/16/2022, 9:22 PM[
{
"id": 1,
"username": "johndoe1",
"email": "<mailto:johndoe1@test.com|johndoe1@test.com>",
"firstName": "",
...
]
kqr
09/16/2022, 9:26 PMdany giguere
09/16/2022, 9:52 PM@GetMapping("/slow-service-users")
fun getAllUsers(): List<User?>? {
// Thread.sleep(2000L) // delay
return userRepository.findAll()
}
@GetMapping(value = ["/users-non-blocking"])
fun getUsersNonBlocking(): Mono<ArrayList<*>> {
<http://logger.info|logger.info>("Starting NON-BLOCKING Controller!")
val userFlux: Mono<ArrayList<*>> = WebClient.create()
.get()
.uri("<http://localhost:8080/slow-service-users>")
.retrieve()
.bodyToMono(ArrayList::class.java)
userFlux.subscribe { user -> <http://logger.info|logger.info>(user.toString()) }
<http://logger.info|logger.info>("Exiting NON-BLOCKING Controller!")
return userFlux
}