提问者:小点点

无法访问Openshift上已部署的Spring Boot应用程序(war)的endpoint


我正在同时试验Spring boot和openshift,不幸的是,我遇到了一个我无法找到的问题。

我用Spring启动的初始值设定项创建了一个应用程序。创建了几个Rest控制器(你好,世界喜欢)

这是我的应用程序(主)

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class Application extends SpringBootServletInitializer {

Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
    Class[] sources = {Application.class};
    SpringApplication.run(sources, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    // Customize the application or call application.sources(...) to add sources
    return application.sources(Application.class);
}

/* It retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out. */
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

        StringBuilder builder = new StringBuilder();

        builder.append("Let's inspect the beans provided by Spring Boot:\n");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            builder.append(beanName);
            builder.append("\n");
        }

        logger.info(builder.toString());
    };
}
}

我可以在本地启动应用程序(通过智能和冬虫)。我可以看到应用程序日志记录并访问所有endpoint,没有任何问题。

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>com.company.example</groupId>
<artifactId>springbootseed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>springbootseed</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<!-- Repositories -->
<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>

    <repository>
        <id>jcenter-snapshots</id>
        <name>jcenter</name>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
<!-- end repositories -->

<profiles>
    <profile>
        <id>local</id>
        <!--  mvn help:active-profiles to find out active profiles-->
        <activation>
            <property>
                <name>env.SPRING_BOOT_ACTIVE_PROFILE</name>
                <value>!dev</value>
            </property>
        </activation>

        <dependencies>

            <!--embedded tomcat server-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>

        </dependencies>
    </profile>

    <profile>
        <id>openshift</id>

        <activation>
            <property>
                <name>env.SPRING_BOOT_ACTIVE_PROFILE</name>
                <value>dev</value>
            </property>
        </activation>

        <dependencies>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>

        </dependencies>

    </profile>
</profiles>

<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>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!--embedded tomcat server-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- API documentation -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

    <!-- JSR-validation-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-bean-validators</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- end API documentation -->

    <!-- Lombok project. Avoids to write boilerplate code-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive. -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <!--<outputDirectory>target</outputDirectory>-->
                <!--<warName>ROOT</warName>-->
            </configuration>
        </plugin>

        <!--  Useful if you want to run source analysis tools on your source after lombok has been applied, -->
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.18.2.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

    </plugins>
</build>

因此,根据我的理解,WAR文件是根据Openshift(minishift)中的LOGS正确创建和部署的:

22:43:39,067 INFO  [stdout] (ServerService Thread Pool -- 64) 
22:43:39,068 INFO  [stdout] (ServerService Thread Pool -- 64)   .   ____          _            __ _ _
22:43:39,069 INFO  [stdout] (ServerService Thread Pool -- 64)  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
22:43:39,076 INFO  [stdout] (ServerService Thread Pool -- 64) ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
22:43:39,076 INFO  [stdout] (ServerService Thread Pool -- 64)  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
22:43:39,076 INFO  [stdout] (ServerService Thread Pool -- 64)   '  |____| .__|_| |_|_| |_\__, | / / / /
22:43:39,077 INFO  [stdout] (ServerService Thread Pool -- 64)  =========|_|==============|___/=/_/_/_/
22:43:39,078 INFO  [stdout] (ServerService Thread Pool -- 64)  :: Spring Boot ::        (v2.0.6.RELEASE)
22:43:39,078 INFO  [stdout] (ServerService Thread Pool -- 64) 
22:43:39,184 INFO  [com.company.example.springbootseed.Application] (ServerService Thread Pool -- 64) Starting Application on springbootseed-openshift-12-t9l5v with PID 194 (started by ? in /opt/app-root/src)
22:43:39,185 INFO  [com.company.example.springbootseed.Application] (ServerService Thread Pool -- 64) The following profiles are active: dev
22:43:39,216 INFO  [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext] (ServerService Thread Pool -- 64) Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@32a0faa2: startup date [Thu Nov 01 22:43:39 UTC 2018]; root of context hierarchy
22:43:41,497 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 64) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
22:43:41,571 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 64) Initializing Spring embedded WebApplicationContext
22:43:41,571 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 64) Root WebApplicationContext: initialization completed in 2355 ms
22:43:42,054 INFO  [org.springframework.boot.web.servlet.ServletRegistrationBean] (ServerService Thread Pool -- 64) Servlet dispatcherServlet mapped to [/]
22:43:42,061 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 64) Mapping filter: 'characterEncodingFilter' to: [/*]
22:43:42,062 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 64) Mapping filter: 'errorPageFilter' to: [/*]
22:43:42,062 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 64) Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
22:43:42,063 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 64) Mapping filter: 'httpPutFormContentFilter' to: [/*]
22:43:42,063 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 64) Mapping filter: 'requestContextFilter' to: [/*]
22:43:42,791 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/log/],methods=[GET]}" onto public java.lang.String com.company.example.springbootseed.controllers.LoggingController.index()
22:43:42,792 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/hello/],methods=[GET]}" onto public java.lang.String com.company.example.springbootseed.controllers.HelloWorldController.sayHello()
22:43:42,800 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/persons/],methods=[GET],produces=[application/json]}" onto public java.util.List com.company.example.springbootseed.controllers.PersonController.getAllPersons()
22:43:42,801 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/persons/{id}],methods=[GET],produces=[application/json]}" onto public com.company.example.springbootseed.domain.Person com.company.example.springbootseed.controllers.PersonController.getPersonById(int)
22:43:42,801 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/persons/{id}],methods=[DELETE]}" onto public void com.company.example.springbootseed.controllers.PersonController.deletePerson(int)
22:43:42,803 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/persons/],methods=[POST],produces=[application/json]}" onto public com.company.example.springbootseed.domain.Person com.company.example.springbootseed.controllers.PersonController.createPerson(com.company.example.springbootseed.domain.Person)
22:43:42,812 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/swagger-resources]}" onto public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
22:43:42,813 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/swagger-resources/configuration/ui]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
22:43:42,813 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/swagger-resources/configuration/security]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
22:43:42,819 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
22:43:42,820 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
22:43:42,992 INFO  [springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping] (ServerService Thread Pool -- 64) Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
22:43:43,103 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 64) Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
22:43:43,266 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 64) Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@32a0faa2: startup date [Thu Nov 01 22:43:39 UTC 2018]; root of context hierarchy
22:43:43,313 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 64) Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
22:43:43,313 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 64) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
22:43:43,326 INFO  [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] (ServerService Thread Pool -- 64) Detected @ExceptionHandler methods in requestValidationErrorHandler
22:43:43,360 INFO  [org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping] (ServerService Thread Pool -- 64) Adding welcome page: ServletContext resource [/index.html]
22:43:43,494 INFO  [org.springframework.jmx.export.annotation.AnnotationMBeanExporter] (ServerService Thread Pool -- 64) Registering beans for JMX exposure on startup
22:43:43,514 INFO  [org.springframework.context.support.DefaultLifecycleProcessor] (ServerService Thread Pool -- 64) Starting beans in phase 2147483647
22:43:43,515 INFO  [springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper] (ServerService Thread Pool -- 64) Context refreshed
22:43:43,538 INFO  [springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper] (ServerService Thread Pool -- 64) Found 1 custom documentation plugin(s)
22:43:43,575 INFO  [springfox.documentation.spring.web.scanners.ApiListingReferenceScanner] (ServerService Thread Pool -- 64) Scanning for api listing references
22:43:44,011 INFO  [com.company.example.springbootseed.Application] (ServerService Thread Pool -- 64) Started Application in 5.643 seconds (JVM running for 18.229)
22:43:44,019 INFO  [com.company.example.springbootseed.Application] (ServerService Thread Pool -- 64) Let's inspect the beans provided by Spring Boot:
[...]
helloWorldController
[...]
personController
[...]
22:43:44,206 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 64) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/springbootseed-0.0.1-SNAPSHOT'
22:43:47,812 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 64) WFLYUT0021: Registered web context: /springbootseed-0.0.1-SNAPSHOT
22:43:47,895 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "springbootseed-0.0.1-SNAPSHOT.war" (runtime-name : "springbootseed-0.0.1-SNAPSHOT.war")
22:43:48,346 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://0.0.0.0:9990/management 
22:43:48,353 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://0.0.0.0:9990 
22:43:48,354 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 22252ms - Started 506 of 755 services (400 services are lazy, passive or on-demand)

正如您在日志中看到的,应用程序似乎被正确加载了。Application类上的日志记录报告加载的beans的列表(为了简洁起见,我将其缩短了)。Spring boot还记录endpoint的注册,但是当我试图访问minishift公开的登录页面时,我得到了标准的wildfly登录页面,当我试图查询控制器时,web服务器返回404 not found。

关于哪里出错了/我应该如何调试它,有什么提示吗?

免责声明:提前道歉,我对Spring boot和Openshift没有经验:)如果你需要其他信息,请让我知道。


共1个答案

匿名用户

应用程序未部署在 Web 服务器的根目录上。在文件夹 /src/主/网络应用/WEB-INF/ 中,我不得不为 JBoss 添加一个名为 jboss-web 的部署描述符.xml

<?xml version="1.0" encoding="UTF-8"?>  
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="  
      http://www.jboss.com/xml/ns/javaee  
      http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">  
   <context-root>myapp</context-root>  
</jboss-web>