I have been trying to write a generic controller to improve code re-usability. Below is what I have so far:
public abstract class CRUDController<T> {
@Autowired
private BaseService<T> service;
@RequestMapping(value = "/validation.json", method = RequestMethod.POST)
@ResponseBody
public ValidationResponse ajaxValidation(@Valid T t,
BindingResult result) {
ValidationResponse res = new ValidationResponse();
if (!result.hasErrors()) {
res.setStatus("SUCCESS");
} else {
res.setStatus("FAIL");
List<FieldError> allErrors = result.getFieldErrors();
List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
for (FieldError objectError : allErrors) {
errorMesages.add(new ErrorMessage(objectError.getField(),
objectError.getDefaultMessage()));
}
res.setErrorMessageList(errorMesages);
}
return res;
}
@RequestMapping(method = RequestMethod.GET)
public String initForm(Model model) {
service.initializeForm(model);
return "country"; // how can I make this generic too ?
}
}
T
can be things like Country, Item, Registration and User. The issue I am facing now the autowiring process failed with the following error:
No unique bean of type [com.ucmas.cms.service.BaseService] is defined: expected single matching bean but found 4: [countryServiceImpl, itemServiceImpl, registrationServiceImpl, userServiceImpl].
Is it possible to achieve what I need ? How can I fix this ?