I have a Jersey / Spring REST servlet. I am trying to use URL versioning mechanism to have 2 versions of the same resource.
What is the best way to solve this ?
This is my web.xml I am trying to load up 2 jersey servlets
<servlet>
<servlet-name>REST_V1</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V1</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
This is the V2 mapping
<servlet>
<servlet-name>REST_V2</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V2</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
I have defined 2 spring components, with the same resource path in their respective packages
package com.comp.resource.v1;
#Controller
#Path("/user")
public class User_V1 {
}
For V2
package com.comp.resource.v2;
#Controller
#Path("/user")
public class User_V2 {
}
I am seeing a conflicting URI Template error for the resource /user
Is there a better way to solve this ? Any help wold be appreciated
Seems like the issue is with how spring beans are loaded. In the web.xml if you have contextConfigLocation outside of the Jersey to load up all the beans, then both REST_V1 and REST_V2 servlet conflicts with the same resource name.
Here is what I changed in the application context.
Removed scanning of resource package from global applicationContext.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.*">
<context:exclude-filter type="regex" expression="com.comp.resource.*"/>
</context:component-scan>
Added 2 more applicationContext, for each servlet
applicationContext_V1.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v1"/>
applicationContext_V2.xml
<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v2"/>
Added reference to these applicationContext file in jersey configuration in web.xml
<servlet>
<servlet-name>REST_V1</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v1</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_V1.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V1</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
and for REST_V2
<servlet>
<servlet-name>REST_V2</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.comp.resource.v2</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_V2.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST_V2</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
Related
I am trying to use Jersey 2.x and have a servlet call "myapp", configuration on web.xml is as follows:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.private.myapp.resource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and have a servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/instance/create</url-pattern>
<url-pattern>/instance/list</url-pattern>
</servlet-mapping>
when I request to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list its return 404
but when I change servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then requesting to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list response as expected
can anyone tell what I am missing?
I have a webservlet with Jersey rest API configured. Now I have to convert the servlet to a liferay portlet. How to convert? Like what portlet-class should I specify in my portlet.xml? The below is the web.xml of my servlet.
<servlet>
<servlet-name>charts</servlet-name>
<!--<servlet-class>javax.servlet.http.HttpServlet</servlet-class>-->
<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>com.charts.api.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>charts</servlet-name>
<url-pattern>/charts</url-pattern>
<url-pattern>/charts/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
How to configure my portlet.xml and use rest service with my portal? I have to deploy the portlet in liferay jboss server as well.
Why don't you use a delegate servlet in liferay?
You can create a liferay portlet and in web.xml define your delegate servlet.
Here you've got a definition example:
<servlet>
<servlet-name>buscador</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>com.dummy.servlet.BuscadorServlet</param-value>
</init-param>
<init-param>
<param-name>sub-context</param-name>
<param-value>buscador</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
It will listens on http://yourliferay/delegate/buscador
Hope it helps
I am trying to implement rest webservice using apache cxf (non-spring). I have configured my web.xml and added one end-point address, it works fine but now i want to add one more end-point address or one more service class and I am unable to do it because the second one overrides the first one.
My web.xml like this
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>abc</param-value>
</init-param>
<init-param>
<param-name>jaxrs.address</param-name>
<param-value>/abc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>xyz</param-value>
</init-param>
<init-param>
<param-name>jaxrs.address</param-name>
<param-value>/xyz</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
You can do like this to have multiple endpoints:
web.xml
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>
org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<!-- Multiple resource classes separated with space -->
<param-value>
com.gsdev.Resource1 com.gsdev.Resource2
com.ttdev.bs.BookSelectionsResource
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Resource classes will be like:
#Path("endpoint1/")
public class Resource1
#Path("endpoint2/")
public class Resource2
Now you have different endpoints as
http://host:port/webapp/services/endpoint1/
http://host:port/webapp/services/endpoint2/
I'm using Jersey for restful service deserialization and in the web.xml file it is declared that i must declare the package name where my resources will be. so it ends up looking like this in web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<!-- this is my package name where my jersey resources are kept -->
<param-value>com.vogella.web.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
now onto my question, what if i wanted to have jersey resources in other packages, could i create another web.xml file or how is it done ?
The packages can be comma separated list.
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<!-- this is my package name where my jersey resources are kept -->
<param-value>com.vogella.web.resources, com.vogella.web.resources1</param-value>
</init-param>
I have developed a simple RESTful service using Apache CXF coupled in a web application and is working fine.
I Can access it on "http://localhost:8080/SpringRestProjectJava/api/books/1234" and I am also getting the proper JSON response.
My understanding is that from this link that WADL will be autogenerated.
Is it correct? If yes, how can I see WADL for this service.
This is my web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/CustomSpringConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>org.gsdev.ws.bookservice.BookResource</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>org.gsdev.ws.bookservice.provider.XstreamJsonProvider</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This is my BookResource.java
#Path( "books/{isbn}" )
public class BookResource {
#GET
#Produces ( "application/json" )
public Book getDetails( #PathParam("isbn") String isbn){
if( isbn.equals( "1234" )){
Book book = new Book();
book.setIsbn(isbn);
book.setTitle("Learning web services by Garry");
return book;
}
return null;
}
}
Finally, after keep on trying I am able to make it work. I think its due to the CXFNonSpringJaxrsServlet. I made below changes and I was able to access the autogenerated WADL.
Changes to web.xml
<servlet>
<servlet-name>simplerest</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simplerest</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Added cxf-servlet.xml in WEB-INF
<jaxrs:server id="bookService" address="/bookservice">
<jaxrs:serviceBeans>
<ref bean="bs"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean='jsonProvider' />
</jaxrs:providers>
</jaxrs:server>
<bean id="bs" class="org.gsdev.ws.bookservice.BookResource"/>
<bean id="jsonProvider" class="org.gsdev.ws.bookservice.provider.XstreamJsonProvider"/>
Have you tried:
http://localhost:8080/SpringRestProjectJava/api/books?_WADL
By the way lots of goodies here:
http://cxf.apache.org/docs/jax-rs.html