我正在尝试在 Apache Karaf 4.4.6 中安装 Web 捆绑包
除了控制器之外,一切都很好。
就像看到注释但不注册一样。我在 Jetty 服务器和 Tomcat 中尝试了战争,两者都运行良好,我不知道缺少什么
mvn clean install
然后
bundle:install -s webbundle:file:location.war?Web-ContextPath=tutorial-app
它应该引导您到/home,但这不起作用,并且执行器工作正常。
/**
*
*/
package com.swathisprasad.springboot;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.swathisprasad.springboot.controller.HomeController;
/**
* @author Swathi
*
*/
@Order(1)
@SpringBootApplication
@EnableWebMvc
@Configuration
@ComponentScan("com.swathisprasad.springboot.controller")
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer{
public static void main(String[] args) {
HomeController h = new HomeController();
System.err.println(h.home()+"111");
SpringApplication.run(Application.class, args);
}
}
控制器
@RestController
public class HomeController {
@GetMapping("/home")
public String home() {
System.err.println("Test from deem");
return "deem";
}
}
pom.xml
<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>
<parent>
<groupId>com.swathisprasad.tutorial</groupId>
<artifactId>tutorial-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>tutorial-server</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>com.swathisprasad.tutorial</groupId>
<artifactId>tutorial-web</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<!--
-->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/resources/static/</outputDirectory >
<resources>
<resource>
<directory>${project.parent.basedir}/tutorial-web/src/main/web/dist/np-app/</directory >
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
<warName>tutorial-app</warName>
</configuration>
</plugin>
<!--
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
<warName>tutorial-app</warName>
<instructions>
<Bundle-SymbolicName>tutorial-app</Bundle-SymbolicName>
<Export-Package>
*
</Export-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>src/main/config/bundle.properties</file>
<type>cfg</type>
<classifier>properties</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
您使用:
Web-ContextPath=tutorial-app
根据 OSGi CMPN,128.3.1 WAB 定义:
上下文路径必须始终以斜线('/'\u002F)开头。 Web Extender 不得将包识别为 Web 应用程序,除非其清单中存在 Web-ContextPath 标头并且标头值是该包的有效路径。
更改为:
Web-ContextPath=/tutorial-app
如果这没有帮助,您需要提供日志以及您在 Karaf 中安装了哪些功能(您需要
pax-web-war
和 pax-web-http-jetty
(或 -tomcat
或 -undertow
))。