Can anybody tell me how to make a restful web service with Jersey 2.0 by not using maven. I have searched everywhere and found tutorial for Jersey1.x versions but not for 2.0. Please help
We provide detail answere based on the user answer user2629427. we checked this on windows 7.
Requirement: (brackets indicate version which this example is tested)
Unzip the tomcat & create a below folder structure in tomcat's 'webapps' folder (folder names are case sensitive).
abc
|___ WEB-INF
|____ classes
|____ lib
Put 'Hello.java' and 'MyApplication.java' into 'classes' folder and 'web.xml' into 'WEB-INF' folder.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.king.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Myapplication.java
package com.king;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.king");
}
}
Hello.java
package com.king;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?><hello>Hello Jersey</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html><title>Hi Jersey</title><body><h1>Hello Jersey this is laksys</body></h1></html>";
}
}
Unzip jersey and copy all jar files from api, ext, and lib (not folders) into your apps 'lib' folder.
Now compile the two java files using following command
D:\apache-tc-8\webapps\abc\WEB-INF\classes>javac -d . -cp ..\lib\javax.ws.rs-api-2.0.1.jar;..\lib\jersey-server.jar;..\l ib\jersey-common.jar *.java
Next run the tomcat server
D:\apache-tc-8\bin>startup
In browser address bar type this: http://localhost:8080/abc/rest/hello