当前位置: 首页 > 知识库问答 >
问题:

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

富钧
2023-03-14

我正在同时试验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个答案

夏飞鹏
2023-03-14

应用程序未部署在 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> 
 类似资料:
  • 议题背景: 我开始使用OpenShift的目的是--表面上--通过OpenShift安装和管理Liferay6.2门户。为此,我使用jbossas-7和PostgreSQL-9.2墨盒创建了一个OpenShift应用程序--与“DIY墨盒”方法相比,我目前更喜欢官方墨盒,即在OpenShift上安装应用服务器,特别是JBoss AS7,理想情况下在该应用服务器中安装Liferay。 按照Lifer

  • 我有一个Spring启动Rest服务项目,它在我的本地机器上工作。当我运行应用程序为"Spring Boot App" 我可以通过转到访问rest服务 http://127.0.0.1:8080/persons/all 它返回JSON,因为它应该。 我换了pom。xml打包到war,然后我创建了一个war,作为- war文件名为“myapp-0.0.1-SNAPSHOT.war”,上传后,我试图通

  • 从我开始尝试在OpenShift上部署一个香草jhipster应用程序到现在已经有一个多星期了。我试过Openshift和我们的Openshift私人公司实例,但没有成功。 我尝试了几种方法来做,使用生成器yo jhipster:openshift或手动使用特定的hook。使用生成器,我做了: .........从这里到那里都有很多例外.........

  • 我正试图在heroku上部署一个dropwizard应用程序,但它无法启动。在本地使用“gradle run server config.yml”可以很好地工作 我正在使用gradle进行构建,当我推到heroku时,构建就成功了。我的分级阶段任务依赖于清洁和罐子(胖罐子创建) 我的Procfile有: 我试过没有成功 Web:java$java_opts-jar build/libs/dropw

  • 完成干净的构建后,我将war文件复制到Tomcat的文件夹中。但是部署会发生两次,并且在上下文已经存在的情况下以异常结束。我错过了什么? 非常感谢您的帮助。