First of all ,i am new to spring webflux and trying to do a POC on setting up a reactive spring boot project.i have a use case, where i need to convert the retrieved entity class(PartyDTO) to Mono object(Person : which is a third party business object without constructors and i cant modify it).i googled but unable to find an answer that match my use case.
3rd party object:
public class Person {
// no constructors
private Integer custId;
private String fullname;
private LocalDate date;
//
getters and setters
}
my classes are as follows:
@Table("party")
public class PartyDTO {
@Id
private Integer party_id;
private String name;
private LocalDate start_date;
}
Service class that calls my repository.
@Service
public class ServiceImpl{
@Override
public Mono<Person> getParty(String partyId) {
return
partyRepository.findById(Integer.parseInt(partyId)).flatMap(//mapper to convert PartyDTO to Person goes here);
}
}
i tried to use a flatmap with my custom mapper as shown above, but its not working.Can some one advice me how this can be achieved in a non blocking way(3rd party bean mappers are also fine if it support non blocking approach)
Assuming partyRepository.findById()
returns a Mono , you can simply do
@Service
public class ServiceImpl{
@Override
public Mono<Person> getParty(String partyId) {
return partyRepository.findById(Integer.parseInt(partyId)).map(partyDto->{
Person person = new Person();
person.setName(partyDto.getName());
return Mono.just(person);
});
}
}
You can refer to https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#just-T-