Java servlet redirect between two different jersey REST services -
we have existing jersey rest service in our application(url: /rest/*). sample url looks like: http://xxx:8080/app/rest/company/getdata
based on property need redirect rest call context(url: /newrest/*): sample url like: http://xxx:8080/app/newrest/company/getdata
so, added servlet mapping in web.xml.
so, web.xml looks below snippet.
<servlet> <servlet-name>servletadaptor</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>xxx.httpservice</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servletadaptor</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <filter> <filter-name>securerestfilter</filter-name> <filter-class>xxx.restsecurityfilter</filter-class> </filter> <filter-mapping> <filter-name>securerestfilter</filter-name> <url-pattern>/rest/*</url-pattern> </filter-mapping> <servlet> <servlet-name>newadaptor</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>xxx.newhttpservice</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>newadaptor</servlet-name> <url-pattern>/newrest/*</url-pattern> </servlet-mapping>
i have written filter(to check property on servletadaptor servlet , need redirect newadaptor servlet, if needed. also, final response should sent servlet adaptor servlet using response newadaptor servlet, if necessary(based on property).
i need directions solving issue. please help.
the dofilter method in filter class looks like:
@override public void dofilter(servletrequest req, servletresponse res, filterchain chain) throws servletexception, ioexception { httpservletrequest request = (httpservletrequest) req; string productmode = "b";//hardcoded example string url = request.getrequesturl().tostring(); if(productmode.equals("a")){ chain.dofilter(req, res); }else{ url=url.replace("rest", "newrest"); req.getrequestdispatcher(url).forward(req, res); } }
but requestdispatcher not seem forward request new url.
from understand trying create new services or integrate newer one. ideally can version url's support backward compatibilty in future.e.g. http://{xxx}:8080/app/rest/v1_0/company/getdata,http://{xxx}:8080/app/rest/v2_0/company/getdata etc.
why don't wrap new api call in existing api call? consumer talks api, internally invoke newer api , consume response of it.then either return new object or wrap object around it.
you need re-look @ strategy trying adopt!
Comments
Post a Comment