Jetty 8.1.11 Embedded Java Application :: 2013/07/11 16:03

Jetty를 Embedding하여 RESTful API를 제공하는 Java Application을 작성하였다.

Jetty의 Distribution Version은 8.1.11.

Jersey Version : 1.8

<Main.java>
public class Main {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

//Jetty Server Start for RESTful Service
JettyServer jettyServer = new JettyServer();

if (!jettyServer.isStarted())
jettyServer.start();
}

}

<JettyServer.java>
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;

public class JettyServer {

private Server server;
public JettyServer() {
this(8080);
}
public JettyServer(Integer port) {
server = new Server(port);
server.setStopAtShutdown(true);
server.setSendServerVersion(true);
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
handler.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig("restful_resource_package"))), "/");
server.setHandler(handler);
}
public void setHandler(ContextHandlerCollection contexts) {
server.setHandler(contexts);
}
public void start() throws Exception {
server.start();
}
public void stop() throws Exception {
server.stop();
server.join();
}
public boolean isStarted() {
return server.isStarted();
}
public boolean isStopped() {
return server.isStopped();
}
}

한가지 유의할 사항은, Jersey Annotation을 사용하여 작성할 모든 Resource는 restful_resource_package에 작성되어야 한다는 것이다.

2013/07/11 16:03 2013/07/11 16:03
Trackback Address :: http://www.yongbi.net/trackback/568
[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다
Name
Password
Homepage

Secret
< PREV |  1  |  ...  222  |  223  |  224  |  225  |  226  |  227  |  228  |  229  |  230  |  ...  566  |  NEXT >