嵌入式码头与战争爆发,使用带注释的配置 [英] Embedded Jetty with exploded war, using annotated config
问题描述
我正在学习Java EE 7 Servlet,并尝试部署 hello2
示例,但收效甚微. hello2
由两个servlet和一个映像文件组成.该配置带有注释,并且该项目没有任何web.xml.
I am learning Java EE 7 Servlets and tried to deploy hello2
example from Java EE 7 tutorial using embedded Jetty (v 9.3.7) with little success. hello2
consists of two servlets and an image file. The configuration is annotated and the project does not have any web.xml.
紧随嵌入式Jetty的 WebAppContext
部分例子我创建了这个主类来初始化我的嵌入式服务器:
Following the WebAppContext
part from embedded Jetty examples I created this main class to initiate my embedded server:
public class MyServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
String webappPath = new File(MyServer.class.getProtectionDomain().getCodeSource().getLocation().getFile())
.getParentFile().getParentFile().getAbsolutePath();
WebAppContext webapp = new WebAppContext(webappPath, "");
webapp.setConfigurations(new Configuration[]{
new AnnotationConfiguration()});
server.setHandler(webapp);
server.start();
server.join();
}
}
据我了解,由于Jetty是Java EE Web容器,它应该能够按原样提供Serlvet项目示例,而我只需要指向war文件夹结构即可.以下是项目的结构:
As I understand, since Jetty is a Java EE web container, it should be able to serve the example Serlvet project as-is, and I simply need to point to the war folder structure. The following is the structure of the project:
-- hello2
\-- src
\-- main
+-- java
│ +-- MyServer.java
│ \-- javaeetutorial
│ \-- hello2
│ +-- GreetingServlet.java
│ \-- ResponseServlet.java
\-- webapp
+-- WEB-INF
│ \-- classes
│ +-- MyServer.class
│ \-- javaeetutorial
│ \-- hello2
│ +-- GreetingServlet.class
│ \-- ResponseServlet.class
+-- index.html
\-- resources
\-- images
\-- duke.waving.gif
The hello2
example code can be found here. Here are some parts of GreetingServlet
@WebServlet("/greeting")
public class GreetingServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
....
和 ResponseServlet
@WebServlet("/response")
public class ResponseServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
....
文件被编译为 hello2/webapp/classes/
,因此使 webapp
文件夹成为爆炸的WAR.我添加index.html只是为了测试Jetty是否接好它.结果是当我访问localhost:8080,localhost:8080/greeting或localhost:8080/response时出现错误404
The files are compiled to hello2/webapp/classes/
thus making the webapp
folder an exploded WAR. The index.html is something I added just to test whether Jetty picks it up. The result is that I get error 404 when I visit localhost:8080, localhost:8080/greeting or localhost:8080/response
如果我在 webapp.setConfigurations()
中添加 WebXmlConfiguration
,然后设置 webapp.setResourceBase(webappPath)
之类的资源库,进入Jetty的静态文件服务器.这是因为Jetty然后使用默认的 web.xml
,该默认代码将其自己的servlet出于文件服务目的添加到服务器.但是即使那样,我的带注释的servlet也不会被使用.
If I add WebXmlConfiguration
with webapp.setConfigurations()
and then set the resource base like webapp.setResourceBase(webappPath)
, I manage to get into the Jetty's static file server. This is because Jetty then uses a default web.xml
which adds its own servlets for file serving purpose to the server. But even then my annotated servlets are not picked up.
让Jetty读取带注释的servlet配置的方法是使用 WebAppContext.getMetadata().setWebInfClassesDirs()
显式设置 WEB-INF
目录:>
The way I got Jetty to read the annotated servlet configuration is by setting the WEB-INF
directory explicitly using WebAppContext.getMetadata().setWebInfClassesDirs()
:
webapp.getMetaData().setWebInfClassesDirs(
Arrays.asList(Resource.newResource(
MyServer.class.getProtectionDomain().getCodeSource().getLocation())));
然后,这些servlet会按预期的方式进行响应,但这不会为我的 index.html
或图像文件提供服务.我还将资源库设置为无用.因此,我想要的是Jetty在不使用 web.xml
的情况下为我的Web应用程序提供服务,只需将其指向爆炸的WAR目录即可.显然我缺少了一些东西.
Then, the servlets respond as expected, but this this does not serve my index.html
or the image file. I also set the resource base to no use. So what I want is Jetty to serve my web application without web.xml
, and by simply pointing it to the exploded WAR directory. Clearly I am missing something.
推荐答案
使用...
webapp.setConfigurations(new Configuration[]{
new AnnotationConfiguration()});
将撤消所有现有的重要配置,并且仅启用 AnnotationConfiguration
.
will undo all of the existing important configurations and only have the AnnotationConfiguration
enabled.
难怪它不适合您,使用该设置,缺少加载 WEB-INF/web.xml
的配置,使用 WEB-INF/lib的配置
丢失,等等.
No wonder it isn't working for you, with that setup, the configuration that loads WEB-INF/web.xml
is missing, the configuration that uses WEB-INF/lib
is missing, etc.
您必须适当地修改现有的配置列表,并且有很多示例向您展示.
You have to modify the existing configuration list appropriately, and there's plenty of examples out there to show you this.
由于您没有指定是否具有使用JNDI的批注,是否存在于Web片段中,或者来自Webapp上下文之外(例如容器本身),因此很难指定所需的确切配置.
Since you didn't specify if you have annotations that use JNDI, or exist in web-fragments, or come from outside of the webapp context (such as the container itself), the exact configuration you need is tough to specify.
请参见 https://github.com/jetty-project/embedded-servlet-3.1 项目,以执行此操作的完整项目.
See the https://github.com/jetty-project/embedded-servlet-3.1 project for a complete project that does this.
context.setConfigurations(new Configuration[]
{
new AnnotationConfiguration(),
new WebInfConfiguration(),
new WebXmlConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new EnvConfiguration(),
new PlusConfiguration(),
new JettyWebXmlConfiguration()
});
这是最常见的常规设置,但可能会使您面临不必要的额外配置.
This is a general setup, which is the most common, but might expose you to extra configuration you might not want.
即使您不想要其他组件,也仍然需要将其保留给发现的Web应用程序.否则,您将拥有一个100%手动的Web应用程序,即专门调用 .addServlet()
和 .addFilter()
等.
Even if you don't want the other components, you still need to leave them be for a discovered webapp. Otherwise you have a 100% manual webapp, that you specifically call .addServlet()
and .addFilter()
etc on.
您可能应该改用这种语法.
You should probably use this syntax instead.
private void enableAnnotationScanning(Server server)
{
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
}
因为这将修改现有的配置列表,仅添加 AnnotationConfiguration
as this will modify the existing list of Configurations to just add the AnnotationConfiguration
如果要查看此格式的其他示例,请查看以下示例项目:
If you want to see other examples of this format, look at the following example projects:
- https://github.com/jetty-project/embedded-jetty-live-war -设置一个WAR文件,该文件可以通过 https://github.com/jetty-project/embedded-jetty-uber-jar -使用所有Jetty +您的web应用程序设置一个jar文件,此
- https://github.com/jetty-project/embedded-jetty-live-war - setting up a WAR file that can either be run via the command line with its own internal Jetty, or be deployed into a container. This uses the
AnnotationConfiguration
as you want. - https://github.com/jetty-project/embedded-jetty-uber-jar - setting up a single jar file with all of Jetty + your webapp, this uses the
ServletContextHandler
and manual setup like was mentioned above.
这篇关于嵌入式码头与战争爆发,使用带注释的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!