mavenspring-mvcapache-karafkarafkaraf-maven-plugin

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


I am creating SpringMVC application with maven in IntelliJ. I referred this link for creating SpringMVC maven application. here, is a code of

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>

I want to deploy WAR of this application on Karaf container. To generate .war file, I am executing below command inside project directory.

mvn compile
mvn package

This generates .war in /target directory. After that I am copying generated .war file to /deploy directory of karaf. This automatically deploys my bundle in Karaf. On execution of below commands, it shows .war bundle as deployed.

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]

But when I open http://localhost:8181/spring-maven/, it shows HTTP Status 404 – Not Found. Apache Tomcat/8.5.32. I don't have tomcat on my machine. I am not getting where I am going wrong. Please guide me on this issue.

Thank you.


Solution

  • However, Karaf not perform component-scan like Tomcat. Remove these 2 lines <context:component-scan base-package="YourPackageName" /> <mvc:annotation-driven /> and adding <context:annotation-config /> in HelloWeb-servlet.xml worked for me.