2

I have created some web services with a library of code that I have imported into my Spring-based Java web project.

my web.xml:

<servlet>
    <servlet-name>serviceDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/*servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

<servlet-mapping>
        <servlet-name>serviceDispatcher</servlet-name>
        <url-pattern>/services/*</url-pattern>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>

My spring config:

<context:component-scan base-package="project.thc, global.libraries" />

My Service Controller:

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());
    }

}

Service URL:

WebServicesConfig.ServiceUrl.userAuthenticationService = "/authorization/validate";

I am using URL http://localhost:[port]/THC/services/authorization/validate and I keep getting 404 errors.

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.

Is it possible to load web services into a project from a JAR (library)?

Can anyone see errors in my configuration that would prevent the service from being detected by the component scan?

EDIT NOTES: Added serviceDispatcher configuration from web.xml. Added reference for WebServicesConfig.ServiceUrl.userAuthenticationService. Updated example URL for localhost

daddygames
  • 1,880
  • 1
  • 11
  • 18
  • Please post your whole web.xml. Please show what servlet implementation serviceDispatcher maps to. – StvnBrkdll Nov 23 '16 at 19:28
  • Can you add @Path for Controller and method and check if its accessible. Do you have any errors when you boot up the application? – Amit Mahajan Nov 23 '16 at 19:29
  • 1
    Can you check http://stackoverflow.com/questions/1242656/spring-annotation-based-controllers-not-working-if-it-is-inside-jar-file? Maybe this can help you – Daniel Arechiga Nov 23 '16 at 20:03
  • What is the value for WebServicesConfig.ServiceUrl.userAuthenticationService , which u have mapped in ur controller ? – Vasu Nov 23 '16 at 20:27
  • yes it is possible to register controllers from lib in spring, you need to properly configure your dispatcher servlet. – Valath Nov 24 '16 at 05:48
  • I am unsure what you are asking me with @Path. Reference? I updated my post to include the other requested information. – daddygames Nov 28 '16 at 14:53
  • @DanielArechiga thank you! That link provided the answer! When generating the JAR that contains the controllers/services, you must check the "Add Directory Entries" checkbox (in Eclipse IDE). Doing this fixed the issue for me. http://i.imgur.com/RKPongG.png – daddygames Nov 28 '16 at 15:33

0 Answers0