Uppicharla
Uppicharla

Reputation: 494

Accessing Spring Rest Service without Dispatcher Servlet

Here actually i am trying to access my spring based rest full service, I am not configuring DispatcherServlet in web.xml, instead of that i am using ContxtLoaderListener to load my spring configuration file.

From my logs i can see my service is getting initialized, when ever i access the above url, ICallServlet is receiving the request since it has the url-pattern as '/*'(this i can't modify).

Here my problem is i could not able to access my service, request is not reaching my service. without using DispatcherServlet is there any way to invoke my rest service, Some one please help me to resolve this issue.

URL: http://localhost:8080/icall-ui/api/casaOnboarding/pwebXML

Upvotes: 1

Views: 7644

Answers (3)

Uppicharla
Uppicharla

Reputation: 494

Finally i got to know that there is no way (as per my knowledge) of invoking spring rest services without using DispatcherServlet.

Thank you so much @Robert for your valuable suggestions. As per @Robert Comments, I modified my code like below to get it to work.

  • web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>classpath*:controllerServiceContext.xml</param-value>  
    </context-param>
    <listener> 
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>
    <servlet>  
       <servlet-name>iCallUI</servlet-name>  
       <servlet-class>com.ui.ICallServlet</servlet-class>  
    </servlet>  
    <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>  
       <servlet-name>iCallUI</servlet-name>  
       <url-pattern>/*</url-pattern>  
    </servlet-mapping>
    
  • dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:jee="http://www.springframework.org/schema/jee"  
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:mvc="http://www.springframework.org/schema/mvc" 
           xsi:schemaLocation="http://www.springframework.org/schema/beans   
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
               http://www.springframework.org/schema/jee   
               http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
               http://www.springframework.org/schema/mvc      
               http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
    
        <context:component-scan base-package="mypackage"/>
        <mvc:annotation-driven />
    
     </beans>
    
  • ControllerServiceContext.xml

I removed below lines of code and left as it is with old code (this file contains some other stuff related to the project).

<context:component-scan base-package="mypackage"/>
<task:annotation-driven />
  • Log file

After seeing the below statement in logs, I can say my service is ready to serve requests.

15:12:01,782 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 58) Mapped "{[/api/casaOnboarding/pwebXML],methods=[POST],params=[],headers=[],consumes=[application/json || application/xml],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity mypackage.CasaOnboardingRestService.pwebXML(mypackage.OnboardingReq,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  • URL - I used below url to access the service

    http://localhost:8080/icall-ui/api/api/casaOnboarding/pwebXML
    

Upvotes: 2

Harinath
Harinath

Reputation: 4199

By using filter in web.xml

    <!-- Spring security -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
                  org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Upvotes: 0

Robert Moskal
Robert Moskal

Reputation: 22553

I'm sorry, but you can't dispatch spring mvc views without a Dispatcher Servlet. Your context will be loaded via the ContextLoaderListener, but just as you've discovered, your routes will never be called.

You could do something like mapping the dispatcher servlet to your api endpoints and then map iCallUI to catch the default route "/" as opposed to "/*":

  <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>  
       <servlet-name>iCallUI</servlet-name>  
       <url-pattern>/</url-pattern>  
  </servlet-mapping>

ICallServlet will replace the default servlet and this may or may not have bad effects depending on how your application is set up. Static file serving may break, for example.

Subclassing org.springframework.web.servlet.DispatcherServlet is an option. But not knowing what you do in com.ui.ICallServlet, who knows how difficult it will be to extend DispatcherServlet.

Also, it seems like the long way around. If you are using Spring to declare your api routes, why not use it to declare them all? Why have two dispatching mechanisms? If you need to do some preprocessing per request then use a Servlet Filter.

Lastly, and perhaps the simplest solution. Just point iCallUI to another url pattern like: "/ui/*".

That pretty much exhausts the possibilities :). Well that and the fact that your controllerServiceContext file isn't set up to parse the url mapping. You also need to add

<mvc:annotation-driven />

Don't forget all the xml namespace info for that!

  xmlns:mvc="http://www.springframework.org/schema/mvc"
  .
  .
   xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   .

Upvotes: 4

Related Questions