Recovery web service with jersey 2.0 without maven

Can someone tell me how to make a calm web service with Jersey 2.0 without using maven. I searched everywhere and found a tutorial for versions of Jersey1.x, but not for 2.0. Please, help

+8
rest web-services jersey jersey-client
source share
3 answers

I found the answer

package com.hellowebservice; 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>" + "Hello Jersey" + "</title>" + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; } } 

web.xml

  <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>FirstRestWebService</display-name> <servlet> <display-name>Rest Servlet</display-name> <servlet-name>RestServlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.hellowebservice.MyApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>RestServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> 

Myapplication.java

 package com.hellowebservice; import org.glassfish.jersey.server.ResourceConfig; public class MyApplication extends ResourceConfig { public MyApplication() { packages("com.hellowebservice"); } } 

run from localhost: 8080 / FirstRestWebService / rest / hello

+1
source share

Just add to the previous answer. If you are not using Maven and just build using Eclipse using the Dynamic Web Project and deploy it to a web application server like Tomcat.

Just download the JIJ-RS 2.0 RI package on Jersey Download the Jersey , unzip and add all the jars to the lib, api and ext folders to your build path. (I tried without emergency cans, but was not recognized at server startup).

Also add all the banks to the deployment assembly of your dynamic web project so that they are automatically copied to the WEB-INF / lib directory when deployed to the web application server. Along with the code and web.xml in the answer above, you should have a RESTful api using Jersey 2 and work.

+1
source share

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

+1
source share

All Articles