<dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.6.v20170531</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.6.v20170531</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-util</artifactId> <version>9.4.6.v20170531</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-jetty-http</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.25.1</version> </dependency> </dependencies>Jetty ve Jersey ile ilgili bağımlılıklar eklenmiştir.
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("msg") public class MyMessage { @GET @Produces(MediaType.TEXT_PLAIN) public String getMessage() { return "My message\n"; } @GET @Path("j") @Produces(MediaType.APPLICATION_JSON) public String getMessageJson() { return "{\"message\":\"My message\"}"; } @GET @Path("jo") @Produces(MediaType.APPLICATION_JSON) public MsgObj getMessageJsonObject() { MsgObj msgObj=new MsgObj(); msgObj.setName("World"); msgObj.setTitle("Hello"); return msgObj; } }Üç tane servis metodu tanımlanmıştır ve GET ile mesaj ve nesne döndürmektedirler. Servis hazır olduğunda göre artık uygulamayı yapabiliriz :
import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.glassfish.jersey.servlet.ServletContainer; public class Main { public static void main(String[] args) { Server server = new Server(8080); ServletContextHandler ctx = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); ctx.setContextPath("/"); server.setHandler(ctx); ServletHolder serHol = ctx.addServlet(ServletContainer.class, "/rest/*"); serHol.setInitOrder(1); serHol.setInitParameter("jersey.config.server.provider.packages", "com.fibiler.services"); try { server.start(); server.join(); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } finally { server.destroy(); } } }
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-9"> <title>Merhaba Jetty</title> </head> <body>Merhaba Jetty!</body> </html>
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>Bu projeyi Jetty ile çalıştıralım. Console da aşağıdaki gibi output görmeniz gerekir :
2020-07-02 13:55:26.666:INFO::main: Logging initialized @281ms Configuration: D:\Users\zteker\AppData\Local\Temp\eclipseJettyPlugin.config.HelloJetty.xml Version: 9.3.25.v20180904 Context: / Port: 8080 Classpath: D:/Works/Ar-Ge/Workspace/HelloJetty/build/classes 2020-07-02 13:55:27.043:INFO:oejs.Server:main: jetty-9.3.25.v20180904, build timestamp: 2018-09-05T00:11:46+03:00, git hash: 3ce520221d0240229c862b122d2b06c12a625732 2020-07-02 13:55:27.295:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=1ms 2020-07-02 13:55:27.328:WARN:oejs.SecurityHandler:main: ServletContext@o.e.j.w.WebAppContext@7a187f14{/,file:///D:/Works/Ar-Ge/Workspace/HelloJetty/WebContent/,STARTING}{D:\Works\Ar-Ge\Workspace\HelloJetty\WebContent} has uncovered http methods for path: / 2020-07-02 13:55:27.347:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@7a187f14{/,file:///D:/Works/Ar-Ge/Workspace/HelloJetty/WebContent/,AVAILABLE}{D:\Works\Ar-Ge\Workspace\HelloJetty\WebContent} 2020-07-02 13:55:27.364:INFO:oejs.AbstractConnector:main: Started ServerConnector@74a10858{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} 2020-07-02 13:55:27.364:INFO:oejs.Server:main: Started @983ms Jetty startup finished in 0,788 s. Used memory: 5,4 MB of 243,5 MB (3,5 GB maximum) Console available: type "help".Run konfigürasyonunu kontrol ederseniz şu şekilde bir şekilde göreceksiniz : Görüldüğü gibi content path değeri olarak / almış. Bu nedenle doğrudan http://localhost:8080 yazarsanız HelloJetty çalışacaktır : Görüldüğü gibi Jetty plug-in kullanarak herhangi bir web server'a ihtiyacınız olmadan web projelerinizi çalıştırabilirsiniz.
D:\Works\Ar-Ge\Jetty>java -jar jetty-runner-9.0.0.M4.jar HelloJetty.war 2020-07-02 17:55:00.661:INFO:oejr.Runner:main: Runner 2020-07-02 17:55:00.729:INFO:oejs.Server:main: jetty-9.0.0.M4 2020-07-02 17:55:00.755:INFO:oejw.WebInfConfiguration:main: Extract jar:file:/D:/Works/Ar-Ge/Jetty/HelloJetty.war!/ to D:\Users\zteker\AppData\Local\Temp\jetty-0.0.0.0-8080-HelloJetty.war-_-any-\webapp 2020-07-02 17:55:00.941:INFO:oejpw.PlusConfiguration:main: No Transaction manager found - if your webapp requires one, please configure one. 2020-07-02 17:55:01.537:INFO:oejsh.ContextHandler:main: started o.e.j.w.WebAppContext@3f102e87{/,file:/D:/Users/zteker/AppData/Local/Temp/jetty-0.0.0.0-8080-HelloJetty.war-_-any-/webapp/,AVAILABLE}{file:/D:/Works/Ar-Ge/Jetty/HelloJetty.war} 2020-07-02 17:55:01.726:INFO:oejs.ServerConnector:main: Started ServerConnector@a22cb6a{HTTP/1.1}{0.0.0.0:8080}jetty-runner-9.0.0.M4.jar ile HelloJetty.war aynı dizindedir. Artık projeniz çalışabilir Görüldüğü gibi jetty-runner bir web projesini, war dosyasını kullanarak komut satırından kolayca çalıştırılabilmektedir.
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.20</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.20</version> </dependency>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>HelloJSF2_2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.xhtml</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <head> <meta charset="ISO-8859-9"/> <title>Merhaba JSF 2.2</title> </head> <body> <h:outputText value="Merhaba JSF 2.2"></h:outputText> </body> </html>
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.30.v20200611</version> <configuration> <webAppSourceDirectory>${project.basedir}/WebContent</webAppSourceDirectory> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/HelloJettyMavenPlugin</contextPath> </webApp> </configuration> </plugin>
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-9"> <title>Merhaba Jetty</title> </head> <body>Merhaba Jetty!</body> </html>