2
votes

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

2

2 Answers

5
votes

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.

2
votes

As for Java, instead of making all data publicly available, you can create setters/getters for the corresponding fields and create no args constructor.

A more declarative way is to use the Lombok Project annotations.

@Data *// This annotation includes all you need*
@Document("yourDocName")
public class YourClass {

    @Id
    private String id;

    @Field("FieldName")
    private String myField;
}

Hope this is useful to someone :)