We provide detailed answers based on user26's user2629427 response. we checked it on windows 7.
Requirement: (brackets indicate the version verified by this example)
- tomcat (version 8 zip)
- Jersey (2.x)
Unzip tomcat and create the structure of the folders below in the tomcat "webapps" folder (folder names are case sensitive).
abc |___ WEB-INF |____ classes |____ lib
Place "Hello.java" and "MyApplication.java" in the "classes" folder and "web.xml" in the "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 the jersey and copy all the jar files from api, ext and lib (and not into folders) into your applications folder.
Now compile two java files using the 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, start the tomcat server
D:\apache-tc-8\bin>startup
In the browser address bar, enter the following: http: // localhost: 8080 / abc / rest / hello
laksys
source share