I was reading this so I could understand better how to deal with asynchronous requests, and even though I understand what he is doing here:
.map(processor::responseToDatabaseEntity) // Create a persistable entity from the response
.map(priceRepository::save) // Save the entity to the database
I have no idea how these two methods would be implemented. Assuming this very simple code:
public Mono<Response> postMethod(String id, Person json) {
return webClient.post().uri(id).body(Mono.just(json), Person.class).retrieve().bodyToMono(Response.class);
}
And the Response Class, something like:
public class Response implements Serializable{
private static final long serialVersionUID = -4101206629695985431L;
public String statusCode;
public Date date;
public PersonInfo data;
I assume the responseToDatabaseEntity
method would be some sort of transformation from the Response to PersonInfoDTO to the database, and the save
would insert into it. But how could I implement something like that using Method Reference(::) that would work inside the .map()
so I can subscribe() after is what I don't understand.
EDIT: Adding the progress I had but adapting more or less to the example I gave with Person class:
public Mono<Object> postPerson(String id, Person json) {
return webClient.post().uri(id).body(Mono.just(json), Person.class).retrieve().bodyToMono(Response.class).map( c -> {
List<PersonDTO> list = new ArrayList<PersonDTO>();
c.data.getRoles().forEach((r) -> {
PersonDTO dto = new PersonDTO(c.data.getOof(), "something", c.data.getFoo(), r);
list.add(dto);
});
return list;
}).map(personController::savePerson).subscribe(); //what I wanted to do
}
If I do this:
});
return list;
}).map(l -> {
l.
});
l
contains a list of PersonDTO, and if I do this:
.map(l -> {
l.forEach( f -> {
f.
});
});
f
contains a Person.
How should I implement the method to send the entire list to insert in the method or send one person by person to save
. I can't use a static method like PersonController::savePerson
because:
@RestController
public class PersonController {
private final Logger LOG = LoggerFactory.getLogger(getClass());
@Autowired
private final PersonInterface personRepository;
public PersonController(PersonInterface personRepository) {
this.personRepository = personRepository;
}
personRepository
cannot be static
.
EDIT2: Have also tried this, does not work:
public List<PersonDTO> savePerson(List<PersonDTO> list) {
list.forEach(dto -> {
personRepository.save(dto);
});
return list;
}