title: Spring @Controller Web Service from JAR in lib
body: <p>I have created some web services with a library of code that I have imported into my Spring-based Java web project.</p>
  
  <p>my web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;serviceDispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              /WEB-INF/config/*servlet.xml
          &lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;serviceDispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/services/*&lt;/url-pattern&gt;
          &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>My spring config:</p>
  
  <pre><code>&lt;context:component-scan base-package="project.thc, global.libraries" /&gt;
  </code></pre>
  
  <p>My Service Controller:</p>
  
  <pre><code>package global.libraries.webservices;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.ResponseBody;
  
  import global.libraries.util.EntityUtil;
  
  @Controller
  public class THCWebServices {
  
      public static final String SOURCE_NAME = EntityUtil.Reflection.getClassName(new THCWebServices().getClass(), false);
  
      @RequestMapping(value=WebServicesConfig.ServiceUrl.userAuthenticationService, method = RequestMethod.GET, produces = "application/json")
      public @ResponseBody String userAuthenticationService(@RequestParam String key, @RequestParam String project, @RequestParam String username, @RequestParam String password, HttpServletRequest request) {
          WebServiceResponse serviceResponse = new WebServiceResponse(project);
          WebServiceParameters serviceParameters = new WebServiceParameters(request, project, key);
          if (!serviceResponse.isMaintenance()) {
              serviceResponse.addMethodResult(WebServiceMethods.userAuthorizationService(serviceParameters));
          }
          return EntityUtil.convertToJson(serviceResponse, serviceParameters.getLogDirectory());
      }
  
  }
  </code></pre>
  
  <p>Service URL:</p>
  
  <pre><code>WebServicesConfig.ServiceUrl.userAuthenticationService = "/authorization/validate";
  </code></pre>
  
  <p>I am using URL <a href="http://localhost:[port]/THC/services/authorization/validate" rel="nofollow noreferrer">http://localhost:[port]/THC/services/authorization/validate</a> and I keep getting 404 errors.</p>
  
  <p>I have been unable to identify why the controller in the global library is not identified. Other services contained within the local project source load just fine.</p>
  
  <p><strong>Is it possible to load web services into a project from a JAR (library)?</strong></p>
  
  <p><strong>Can anyone see errors in my configuration that would prevent the service from being detected by the component scan?</strong></p>
  
  <p><em>EDIT NOTES: Added serviceDispatcher configuration from web.xml. Added reference for WebServicesConfig.ServiceUrl.userAuthenticationService. Updated example URL for localhost</em></p>