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>
Related
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 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>
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/
When do I need to include the following snippet in web.xml in order to activate Jersey?
<servlet>
<servlet-name>Jersey App</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>com.company.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is it App. Server dependent if Jersey is activated by default or needs this web.xml entry?
If an App. Server does not require the code above, can I configure the servlet-mapping path, like:
<servlet-mapping>
<servlet-name>Jersey App</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I am just summarizing the Jersey 2.x docs sent by Bhesh in comment:
Servlet 2.x Container
(Java EE 5, Glassfish 2.x), you need to hook Jersey as a Servlet.
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
or a Servlet Filter:
<filter>
<filter-name>MyApplication</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
...
</init-param>
</filter>
...
<filter-mapping>
<filter-name>MyApplication</filter-name>
<url-pattern>/myApp/*</url-pattern>
</filter-mapping>
Servlet 3.x Container
Descriptorless case
(Java EE 6, 7; Glassfish 3, 4)There is no need for web.xml configuration for the simplest case. Instead an custom ResourceConfig class is needed with #ApplicationPath annotation for configuration:
#ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("org.foo.rest;org.bar.rest");
}
}
Configuring with web.xml
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/myresources/*</url-pattern>
</servlet-mapping>
</web-app>
In this case all the root resource classes (the classes annotated with #Path annotation) as well as any providers that are annotated with #Provider annotation packaged with the application will be automatically registered in the JAX-RS application.
I followed the guide found here:
https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart
Here is my POM:
<!-- SWAGGER -->
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
<version>1.3.0</version>
</dependency>
The Annotations are found fine, so i am assuming the POM is working OK
For WEB.xml, I tried doing multiple things, following the guide:
<servlet>
<servlet-name>jersey-serlvet</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.thomsonreuters.ips.service;com.wordnik.swagger.jersey.listing</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- SWAGGER serverlet? -->
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/{PROJECTNAME}</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
but this throws a wild error when i try to reach:
localhost:8080/{PROJECTNAME}/service/api-docs
HTTP Status 500 - java.lang.NoSuchMethodError: com.wordnik.swagger.annotations.ApiOperation.authorizations()Ljava/lang/String;
next I saw around the googleverse to modify the web.xml to the following:
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>2.0</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.thomsonreuters.ips.service;com.wordnik.swagger.jaxrs;</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
note the api.version and the new jaxrs init param.
when that happens, and i go to
http://localhost:8080/ScholarlyItemService/service/api-docs
i get no errors, but i do get useless webpage:
{"apiVersion":"0.0","swaggerVersion":"1.2"}
I think you almost have it. What you see is not a useless webpage, it's rather the documentation of your service built by swagger as a json document. You have to annotate your code as shown in the Quickstart guide. This should show up in this document.
What you may actually want is the Swagger UI: http://swagger.wordnik.com/ to see this document in a readable way.
It took me a moment, but I got it running. You have to download from here: https://github.com/wordnik/swagger-ui, and copy the contents from the dist folder to your /webapp folder. You can then open the contained index.html in a browser. In my case, I could not access this document directly, so I had to put the dist' contents in a subfolder (e.g. /docs) and exclude this from my servlet mapping in web.xml as described here: http://blog.ericdaugherty.com/2010/02/excluding-content-from-url-pattern-in.html.
In the index.html change the url: "http://petstore.swagger.wordnik.com/api/api-docs",
part to your api endpoint http://localhost:8080/ScholarlyItemService/service/api-docs and you will get your api documentation when calling http://localhost:8080/ScholarlyItemService/docs/index.html