title: Generic Controller in Spring MVC
body: <p>I have been trying to write a generic controller to improve code re-usability. Below is what I have so far:</p>
  
  <pre><code>public abstract class CRUDController&lt;T&gt; {
  
      @Autowired
      private BaseService&lt;T&gt; 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&lt;FieldError&gt; allErrors = result.getFieldErrors();
              List&lt;ErrorMessage&gt; errorMesages = new ArrayList&lt;ErrorMessage&gt;();
              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 ?
      }
  }
  </code></pre>
  
  <p><code>T</code> can be things like Country, Item, Registration and User. The issue I am facing now the autowiring process failed with the following error:</p>
  
  <pre><code>No unique bean of type [com.ucmas.cms.service.BaseService] is defined: expected single matching bean but found 4: [countryServiceImpl, itemServiceImpl, registrationServiceImpl, userServiceImpl].
  </code></pre>
  
  <p>Is it possible to achieve what I need ? How can I fix this ?</p>