Search code examples
wicketweb.xmlwicket-6spring-restcontrollerspring-rest

How to integrate Spring Rest with Apache Wicket web application


I have Apache Wiket + Spring web application that works without any issues . At present Spring used DI framework and to filter Mobile access . We are planning to use Spring Rest in our application , Can you advise me how do I do this in our existing web xml .
Initially Rest Api will be used by existing Web sessions to access data(ui pages using ajax calls ), Therefore I want Rest controllers able to access existing Wicket http session(if used logged in ) . e.g. : rest call from existing http session should be able to access existing http session . Any idea ? Thanks and appriciate your time.

My idea was to use ‘org.springframework.web.servlet.DispatcherServlet’ with a Tag , however my doubt is this way I want be sharing same session ?

My existing web.xml (working)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:myapp-spring-config.xml
    </param-value>
</context-param>
<filter>
    <filter-name>myapp</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
        <param-name>applicationFactoryClassName</param-name>
        <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
    </init-param>
</filter>

<filter>
   <filter-name>myappMobileRequestFilter</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
   <filter-name>myappMobileRequestFilter</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>REQUEST</dispatcher>
   <dispatcher>ERROR</dispatcher>
</filter-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<listener>
<listener-class>
    org.springframework.web.context.request.RequestContextListener
</listener-class>


Solution

  • Solved for me ! Thanks for martin-g on directing me to WicketSessionFilter . I m posting the answer here for future reference of any one or for suggestion(if needed)..

    Changes I/We made in my 'web.xml' addition to above posted web.xml.

      <filter>
            <filter-name>SessionFilter</filter-name>
            <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
            <init-param>
                <param-name>filterName</param-name>
                <param-value>myapp</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>SessionFilter</filter-name>
            <url-pattern>/rest/*</url-pattern>
        </filter-mapping>
    
        <servlet>
                <servlet-name>mvc-dispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
                <servlet-name>mvc-dispatcher</servlet-name>
                <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    

    As you can see I have added a WicketSessionFilter to to filter /rest/* requests which consume by spring consume by spring DispatcherServlet.

    and if you want to access spring session in Spring rest controller all you have to do is below e.g :

    @RestController
    @RequestMapping("/service")
    public class TestServiceController {
    
    
        @RequestMapping(value = "/getValue/{name}", method = RequestMethod.GET)
        @SuppressWarnings("unused")
        public String getValue(@PathVariable String name,HttpServletRequest request, HttpServletResponse response) {
    
          HttpSession session = request.getSession(false);
          WebSession wicketSession = session.getAttribute("wicket:wicket.myapp:session");
    
              //rest of the code here ........
                     return result;
         }
    

    And Spring-rest & Wicket lived happily ever after ....