Building a RESTful Web Service :: Learn how to create a RESTful web service with Spring. :: spring-boot http://spring.io/guides/gs/rest-service/

Main.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package hello;
  2. import java.util.HashSet;
  3. import org.apache.catalina.Context;
  4. import org.apache.catalina.core.AprLifecycleListener;
  5. import org.apache.catalina.core.StandardServer;
  6. import org.apache.catalina.startup.Tomcat;
  7. public class Main {
  8. public static void main(String[] args) throws Exception {
  9. Tomcat tomcat = new Tomcat();
  10. tomcat.setPort(8080);
  11. tomcat.setBaseDir(".");
  12. tomcat.getHost().setAppBase("/");
  13. // Add AprLifecycleListener
  14. StandardServer server = (StandardServer)tomcat.getServer();
  15. AprLifecycleListener listener = new AprLifecycleListener();
  16. server.addLifecycleListener(listener);
  17. Context context = tomcat.addWebapp("/", "/");
  18. HashSet<Class<?>> classes = new HashSet<Class<?>>();
  19. classes.add(HelloWorldWebAppInitializer.class);
  20. // TODO: The following line is necessary to run hello.Main in Eclipse or from 'gradlew run'.
  21. // But when the executable JAR produced by Maven Shade plugin is run, it results in
  22. // an error from the DispatcherServlet being loaded twice with the name "dispatcher".
  23. // Need to find a way that works equally well with 'gradlew run'/Eclipse and the
  24. // executable JAR.
  25. // context.addServletContainerInitializer(new SpringServletContainerInitializer(), classes);
  26. tomcat.start();
  27. tomcat.getServer().await();
  28. }
  29. }