Qualcuno può dirmi come creare un servizio Web riposante con Jersey 2.0 evitando di utilizzare Maven. Ho cercato ovunque e ho trovato tutorial per le versioni di Jersey1.x ma non per 2.0. Please helpRestful webservice with jersey 2.0 without maven
risposta
ho trovato la risposta
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");
}
}
corsa con localhost: 8080/FirstRestWebService/resto/ciao
Quali sono i JAR inclusi? Sei riuscito a restituire oggetti complessi invece di stringhe? –
Ho provato questo esempio ma quando prendo l'URL si dice "HTTP Status 404 -/FirstRestWebService/rest/hello" – Chetan
puoi chiarire i pls, che significa eseguire con "localhost ..." dov'è il server che inizia? dov'è il metodo principale? – Gobliins
Solo per aggiungere alla risposta precedente. Se non si utilizza Maven e si sta semplicemente costruendo utilizzando Eclipse con un progetto Web dinamico e distribuendolo su un server di app Web come Tomcat.
È sufficiente scaricare il bundle Jersey JAX-RS 2.0 Jersey Downloads, decomprimere e aggiungere tutti i contenitori nelle cartelle lib, api ed est al percorso di build. (Ho provato senza ext jar, ma ho ottenuto classnotfound all'avvio del server).
Inoltre, aggiungere tutti i vasi al gruppo di distribuzione del progetto Web dinamico in modo che vengano copiati automaticamente nella directory WEB-INF/lib quando distribuiti sul server di applicazioni Web. Insieme al codice & web.xml nella risposta sopra, dovresti avere un api RESTful usando il Jersey 2 attivo e funzionante.
Noi forniamo i dettagli answere in base alla risposta dell'utente user2629427. abbiamo controllato questo su Windows 7.
Presupposto: (tra parentesi indicano la versione che questo esempio viene testato)
- Tomcat (versione 8 zip)
- Jersey (2.x)
Decomprimi il tomcat & crea una struttura di cartelle sotto nella cartella 'webapps' di tomcat (i nomi delle cartelle fanno distinzione tra maiuscole e minuscole).
abc
|___ WEB-INF
|____ classes
|____ lib
Put 'Hello.java' e 'MyApplication.java' nella cartella 'classi' e 'web.xml' nella cartella 'WEB-INF'.
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 e copiare tutti i file jar via api, ext, e lib (non cartelle) nella cartella "lib" delle app.
Ora compilare i due file java usando seguente comando
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
Avanti eseguire il server Tomcat
D:\apache-tc-8\bin>startup
Nel tipo di browser barra degli indirizzi in questo modo: http://localhost:8080/abc/rest/hello strumento
Che accumulo vuoi uso? –
Uso solo eclipse – user2629427
Eclipse è un IDE, non uno strumento di creazione. Se non vuoi usare Maven, probabilmente usi Ant, o Gradle, ... Quale di questi usi? Quale di questi usi * Eclipse *? –