Start java application with jetty without WAR file

2019-09-01 07:14发布

I try start Jersey + Jetty + Nginx via this tutorial and I cannot use war file. How can I start my java application?

I start application by right click on BackendServer.java and click "Run" in IDEA or using in terminal java -cp /home/example/backend/build/WEB-INF/lib/backend.jar:/home/example/backend/libs/* com.example.backend.BackendServer.

Project structure is described here.

/opt/jetty/webapps/backend.xml

<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC
    "-//Mort Bay Consulting//DTD Configure//EN"
    "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!--
  Configure a custom context for serving javadoc as static resources
-->

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>??????????</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="welcomeFiles">
        <Array type="String">
          <Item>index.html</Item>
        </Array>
      </Set>
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
</Configure>

What should be instead ???????? Should I use embedded jetty in BackendServer.java? I know that I have redirect requests from nginx to jetty, but I don't understand how can I start jersey application with jetty...

1条回答
祖国的老花朵
2楼-- · 2019-09-01 07:38

The linked example/tutorial and the linked earlier question are not compatible.

The tutorial is for Jetty 6 (now impossibly out of date), and uses embedded-jetty completely, with deployments and everything enabled.

Your prior question sets up a com.sun.net.httpserver.HttpServer which is not the same thing.

resourceBase is the root directory for any webapp content that you might want to serve.

Since you are using a simple ContextHandler, then that should point to a directory on your disk.

If you were using a WebAppContext, then that should point to your webapp base directory (where optional files like WEB-INF/web.xml or WEB-INF/classes would be)

The ResourceHandler you have defined should use the ContextHandler.resourceBase.

Be aware that ResourceHandler is for the barest, most simplistic static file serving. If you have any requirements from your web clients to perform cached lookups, resume downloads, partial downloads, or mime-type controls, then use a DefaultServlet.

Also, if all you want is a file server in embedded jetty, why are you using an XML deployment with a basic ContextHandler? That would be so much easier to just write into your embedded jetty service.

Some embedded jetty example code that might prove useful to you:

查看更多
登录 后发表回答