Apache Karaf WAR 文件部署不工作。 404-在网络浏览器上找不到

Apache Karaf WAR file deploy not working. 404-Not found on webbrowser

我正在 IntelliJ 中使用 maven 创建 SpringMVC 应用程序。我参考 this link 创建 SpringMVC maven 应用程序。这里是

的代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>temp-maven</groupId>
    <artifactId>spring-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <spring.version>5.1.0.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

    </dependencies>
</project>

web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!--<web-app xmlns=”http://java.sun.com/xml/ns/j2ee"-->
        <!--xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"-->
        <!--xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"-->
        <!--version=”2.4″>-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>HelloWorld Application</display-name>

    <description>
        This is a simple web application for karaf deployment test.
    </description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我想在 Karaf 容器上部署此应用程序的 WAR。要生成 .war 文件,我在项目目录中执行以下命令。

mvn compile
mvn package

这会在 /target 目录中生成 .war。 之后,我将生成的 .war 文件复制到 karaf/deploy 目录。这会自动在 Karaf 中部署我的包。在执行以下命令时,它显示 .war 捆绑包已部署。

karaf@root()> web:list

ID  │ State       │ Web-State   │ Level │ Web-ContextPath │ Name
────┼─────────────┼─────────────┼───────┼─────────────────┼──────────────────────────────
151 │ Active      │ Deployed    │ 80    │ /spring-maven   │ spring-maven (1.0.0.SNAPSHOT)

karaf@root()> http:list

ID  │ Servlet               │ Servlet-Name             │ State       │ Alias               │ Url
────┼───────────────────────┼──────────────────────────┼─────────────┼─────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
59  │ TomcatResourceServlet │ /system/console/res:/res │ Deployed    │ /system/console/res │ [/system/console/res/*]
59  │ KarafOsgiManager      │ ServletModel-6           │ Deployed    │ /system/console     │ [/system/console/*]
151 │ TomcatResourceServlet │ default                  │ Deployed    │ /spring-maven/      │ [/spring-maven/]
151 │ DispatcherServlet     │ dispatcher               │ Deployed    │                     │ [/spring-maven/]
151 │ JspServletWrapper     │ jsp                      │ Deployed    │                     │ [/spring-maven/*.jsp, /spring-maven/*.jspx, /spring-maven/*.jspf, /spring-maven/*.xsp, /spring-maven/*.JSP, /spring-maven/*.JSPX, /spring-maven/*.JSPF, /spring-maven/*.XSP]

但是当我打开http://localhost:8181/spring-maven/时,它显示HTTP Status 404 – Not Found. Apache Tomcat/8.5.32。我的机器上没有 tomcat。我没有得到我要去哪里错了。请指导我解决这个问题。

谢谢。

然而,Karaf 不像 Tomcat 那样执行组件扫描。 删除 这两行 <context:component-scan base-package="YourPackageName" /> <mvc:annotation-driven /> 并且 添加 <context:annotation-config />HelloWeb-servlet.xml 为我工作。