Невозможно развернуть веб-приложение Spring MVC в heroku (в пути к классам не обнаружено типов Spring WebApplicationInitializer) - PullRequest
0 голосов
/ 19 апреля 2020

Вот мои коды конфигурации для использования приложения Spring MVC с swagger

package com.docs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan({"com.docs"})
public class SpringConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver
                = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}
package com.docs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

        @Bean
        public Docket api(){
            return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
        }

        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                .title("DIGITAL-Doc-Service")
                .description("Swagger api documentation of exposed rest apis of service")
                .version("1.0-SNAPSHOT")
                .termsOfServiceUrl("http://terms-of-services.url(to be updated)")
                .license("LICENSE(to be updated)")
                .licenseUrl("http://url-to-license.com(to be updated)")
                .build();
        }

}

package com.docs.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SwaggerConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

}
package com.docs.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api("Sample controller")
public class WelcomeController {

    private final Logger logger = LoggerFactory.getLogger(WelcomeController.class);

    @ApiOperation("Test for get api")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<String> index() {
        return ResponseEntity.ok("Hello World");


    }

    private String getMessage() {
        return "Hello World";
    }

}

Во время работы с eclipse и его развертывания в tomcat обнаруживается WebApplicationInitializer При развертывании в Heroku печатается следующая информация о журнале, и сервлет диспетчера не инициализируется

 Build started by user utsab.c97@gmail.com
2020-04-19T11:40:00.887879+00:00 app[api]: Deploy 15bb1a74 by user utsab.c97@gmail.com
2020-04-19T11:40:00.887879+00:00 app[api]: Release v5 created by user utsab.c97@gmail.com
2020-04-19T11:40:01.525847+00:00 heroku[web.1]: State changed from down to starting
2020-04-19T11:40:05.000000+00:00 app[api]: Build succeeded
2020-04-19T11:40:07.952327+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2020-04-19T11:40:07.961643+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2020-04-19T11:40:08.632914+00:00 app[web.1]: Expanding digital-doc-service.war into /app/target/tomcat.11257/webapps/expanded
2020-04-19T11:40:08.633101+00:00 app[web.1]: Adding Context  for /app/target/tomcat.11257/webapps/expanded
2020-04-19T11:40:09.419262+00:00 heroku[web.1]: State changed from starting to up
2020-04-19T11:40:09.192830+00:00 app[web.1]: Apr 19, 2020 11:40:09 AM org.apache.coyote.AbstractProtocol init
2020-04-19T11:40:09.192840+00:00 app[web.1]: INFO: Initializing ProtocolHandler ["http-nio-11257"]
2020-04-19T11:40:09.245100+00:00 app[web.1]: Apr 19, 2020 11:40:09 AM org.apache.catalina.core.StandardService startInternal
2020-04-19T11:40:09.245123+00:00 app[web.1]: INFO: Starting service [Tomcat]
2020-04-19T11:40:09.246825+00:00 app[web.1]: Apr 19, 2020 11:40:09 AM org.apache.catalina.core.StandardEngine startInternal
2020-04-19T11:40:09.246826+00:00 app[web.1]: INFO: Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-04-19T11:40:09.458971+00:00 app[web.1]: Apr 19, 2020 11:40:09 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
2020-04-19T11:40:09.458988+00:00 app[web.1]: INFO: No global web.xml found
2020-04-19T11:40:14.006181+00:00 app[web.1]: Apr 19, 2020 11:40:14 AM org.apache.catalina.core.ApplicationContext log
2020-04-19T11:40:14.006191+00:00 app[web.1]: INFO: No Spring WebApplicationInitializer types detected on classpath
2020-04-19T11:40:14.161786+00:00 app[web.1]: Apr 19, 2020 11:40:14 AM org.apache.jasper.servlet.TldScanner scanJars
2020-04-19T11:40:14.161789+00:00 app[web.1]: INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-04-19T11:40:14.247100+00:00 app[web.1]: Apr 19, 2020 11:40:14 AM org.apache.coyote.AbstractProtocol start
2020-04-19T11:40:14.247104+00:00 app[web.1]: INFO: Starting ProtocolHandler ["http-nio-11257"]
2020-04-19T11:40:14.442981+00:00 heroku[router]: at=info method=GET path="/" host=obscure-depths-27206.herokuapp.com request_id=cc889e00-ec09-4639-9d37-b3cc5e03d496 fwd="202.8.112.160" dyno=web.1 connect=0ms service=2206ms status=404 bytes=864 protocol=https
2020-04-19T11:40:15.119066+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=obscure-depths-27206.herokuapp.com request_id=bc1b7a17-be3b-4916-b9ba-0ff2da41bfac fwd="202.8.112.160" dyno=web.1 connect=0ms service=6ms status=404 bytes=875 protocol=https

Вот этот файл Heroku

web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

И это 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.digitalfiles.service</groupId>
    <artifactId>digital-doc-service</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>digital-doc-service Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <!-- https://maven.apache.org/general.html#encoding-warning -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.0.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- logging , spring 5 no more bridge, thanks spring-jcl -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- junit 5, unit test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>

        <!-- unit test -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!-- for web servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Some containers like Tomcat don't have jstl library -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.2</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>digital-doc-service</finalName>
        <plugins>
            <!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.12.v20180830</version>
            </plugin>

            <!-- Default is too old, update to latest to run the latest Spring 5 + 
                jUnit 5 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

            <!-- Default 2.2 is too old, update to latest -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <!-- For Heroku embedded deployment -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.heroku</groupId>
                                    <artifactId>webapp-runner</artifactId>
                                    <version>9.0.30.0</version>
                                    <destFileName>webapp-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

Project Directory Structure _image_

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...