In a SpringBoot application I have a service (in the Spring sense) that calls clients that themselves consume RestFul web services by calling a function that is identical to createXXX except that the nature of the object changes at the input of the method. To simplify, let's suppose that I model the creation of animals: with the objects CatDTO, DogDTO, HorseDTO as input of the web services. AnimaslDTO is an object composed of different types of animals (there is no inheritance relationship).
I make the three clients in the same manner let's take the cat's example
package com.animals.client;
import ...
@Service
public class CatClient {
private static final String CODE_WS_CREATE_CAT = "createCat";
/**
* Call RestFul WS for Cat Creation
* @param catDTO
*/
public ResponseEntity<String> createCat(CatDTO catDTO)
{
// ... Call a RestFul WS in the same manner for each animal. Only the parameter change in each client
// --> ie dogDTO in DogClient.createDog, HorseDTO in HorseClient.createHorse,
}
}
In AnimalService implemented by AnimalServiceImpl I try to create the three animals Here is the code
package com.animals.service.impl;
import ...
@Service
public class AnimalServiceImpl implements AnimalService {
@Autowired
private CatClient catClient;
@Autowired
private DogClient dogClient;
@Autowired
private HorseClient horseClient;
@Override
public ResponseEntity<String> createAnimals(AnimalsDTO animalsDTO) {
catClient.createCat(animalsDTO.getCat());
dogClient.createDog(animalsDTO.getDog());
horseClient.createHorse(animalsDTO.getHorse());
....
}
}
I want to know how to generify my clients in Spring or Java for the methods which are very similar as createXXX . What design pattern can I use ? What I've tried is to use Java generics but it doesn't fit well with Spring. I've tried to create a AnimalClient(T) class where T is the animal but I have problems in autowiring constructor with one argument.
Thanks by advance
Aucun commentaire:
Enregistrer un commentaire