Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webflux returns a list of empty objects

I'm new to programming with Spring. I am testing reactive programming with the Reactor / Webflux project.

Database registration works well via a Post.

But every API call Get Mono or Flux I get a list of empty objects. Could you please give me a solution?

I initiated the project via Spring boot / Maven in Kotlin and Mongo reactive.

Thank you for your help !

My UsersApplication

@SpringBootApplication
class UsersMsApplication

fun main(args: Array<String>) {
    runApplication<UsersMsApplication>(*args)
}

My model :

@Document(collection = "user")
data class User(
                @Id
                private var id: String,
                @Field("name")
                private var name: String
                ) : Serializable

My repository

@Repository
interface UserRepository : ReactiveMongoRepository<User, String>

My UserService

interface UserService {
    fun createUser(user: User): Mono<User>

    fun findAllUsers(): Flux<User>

    fun findOneById(id: String): Mono<User>
}

My UserServiceImpl

@Service
class UserServiceImpl(private val userRepository: UserRepository): UserService {
    override fun createUser(user: User): Mono<User> {
        return userRepository.save(user)
    }

    override fun findAllUsers(): Flux<User> {
        return userRepository.findAll()
    }

    override fun findOneById(id: String): Mono<User> {
        return userRepository.findById(id)
    }
}

My UserRessource

@RestController
@RequestMapping("/api")
class UserResource(private val userService: UserService) {

    @PostMapping("/users")
    fun addUser(@RequestBody user: User) : Mono<User> = userService.createUser(user)

    @GetMapping("/users")
    fun findAllUsers() : Flux<User> = userService.findAllUsers()

    @GetMapping("/users/{id}")
    fun getOneUser(@PathVariable id: String) : Mono<User> = userService.findOneById(id)

}

My application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=users_db

logging.level.org.springframework.data=debug

I have three entries in the database but here is what I returned as results by postman, a curl or the browser :

[
    {},
    {},
    {}
]

and in my console :

2019-05-07 15:05:13.368 DEBUG 25022 --- [ctor-http-nio-4] o.s.d.m.core.ReactiveMongoTemplate : find using query: { } fields: Document{{}} for class: class com.inovans.backend.usersms.domains.User in collection: user

like image 803
R. I Avatar asked Nov 02 '25 00:11

R. I


1 Answers

In your User data class all your members are declared as private, so won't be serialized to JSON when returned to the client. Change them to public (just remove the word private, as it'll default to public) and hopefully that'll help.

like image 57
Yoni Gibbs Avatar answered Nov 03 '25 14:11

Yoni Gibbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!