10

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 ?

abiieez
  • 3,139
  • 14
  • 57
  • 110
  • 2
    As of Spring 4, it is possible to autowire based on generic type, see update in my answer. – matsev Nov 01 '13 at 21:49
  • 1
    How do you make the subclass have different `@RequestMapping` (i.e. url)? Now it seems that all children visit the same url `"/validation.json"`... Thanks! – ch271828n Feb 11 '20 at 00:56

2 Answers2

12

I suggest that you add the BaseService as a constructor parameter to the CRUDController class:

public abstract class CRUDController<T> {

    private final BaseService<T> service;
    private final String initFormParam;

    public CRUDController(BaseService<T> service, String initFormParam) {
        this.service = service;
        this.initFormParam;
    }

    @RequestMapping(value = "/validation.json", method = RequestMethod.POST)
    @ResponseBody
    public ValidationResponse ajaxValidation(@Valid T t, BindingResult result) {
        // same as in the example
        return res;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initForm(Model model) {
        service.initializeForm(model);
        return initFormParam;   // Now initialized by the constructor
    }
}

Then you can use autowiring for each of the subclasses that extend it:

public class CountryController extends CRUDController<Country> {

    @Autowired
    public CountryController(CountryService countryService) {
        super(countryService, "country");
    }
}

Alternatively, you can use the @Qualifier annotation in your constructors to distinguis between different BaseService implementation:

@Autowired
public CountryController(@Qualifier("countryServiceImpl") BaseService<Country> baseService) {
    super(baseService, "country");
}

Update:

As of Spring 4.0 RC1, it is possible to autowire based on generic type. Consequently, you can use a generic BaseService<Country> as a parameter when autowiring your constructor, and Spring will still be able to figure out which is the correct one without throwing any NoSuchBeanDefinitionException:

@Controller
public class CountryController extends CRUDController<Country> {

    @Autowired
    public CountryController(BaseService<Country> countryService) {
        super(countryService, "country");
    }
}
matsev
  • 32,104
  • 16
  • 121
  • 156
  • How do you make the subclass have different `@RequestMapping` (i.e. url)? Now it seems that all children visit the same url `"/validation.json"`... Thanks! – ch271828n Feb 11 '20 at 00:56
5

Here is my solution

public abstract class CrudController<Model extends MyEntity<Model>, Service extends SharedService>{

private Service service;

public void setDependencies(Service service){
    this.service = service;
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public Response get(){
    Response response = new Response();
    try{
        response.setStatusCode(200);
        response.setMessage("Successfully retrieved " + service.count() + ".");
        response.setData(service.getAll());
    }catch(Exception e){
        response.setServerError(e.getMessage());
    }
    return response;
}

And in controller you can do something like this

@RequestMapping(value = "/foo")
@RestController
public class FooController extends CrudController<Foo, FooServiceImpl> {

private final FooServiceImpl fooService;
}
@Autowired
public GroupsController(FooServiceImpl fooService){
    super.setDependencies(fooService);
    this.fooService = fooService;
}

Now you can use route mylocalhost/foo/get which will return to you all foos.

Admir Sabanovic
  • 645
  • 1
  • 11
  • 18
  • 1
    How do you make the subclass have different `@RequestMapping` (i.e. url)? Now it seems that all children visit the same url `"/validation.json"`... Thanks! – ch271828n Feb 11 '20 at 00:56