Maven Spring Boot 多模块

4

我正在尝试配置Maven使用Spring Boot与多模块。我对Maven和Spring Boot不是很熟悉,也不确定它们如何协同工作。因此,我开始创建一个简单的结构。一开始,我只想从一个模块开始。

myApp
|
|--web module
|----java
|------controller
|------Application.java (Main Methode)
|----resources
|------templates
|--------index.html
|------application.propperties
|------log4j2.json
|----webapp
|----pom.xml
|--pom.xml

这是我的父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>net.benteler.apps.saplication</groupId>
    <artifactId>saplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>web</module>
    </modules>
    <packaging>pom</packaging>

    <name>saplication</name>
    <description>Shows SAP Reports</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-c3p0</artifactId>
                <version>5.0.1.Final</version>
            </dependency>


            <!-- Database Driver-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>5.1.36</version>
            </dependency>
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc</artifactId>
                <version>6</version>
            </dependency>


            <!-- JSON handling-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.6.2</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.6.2</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.6.2</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>saplication-snapshot</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

以下是我的主模块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">
    <parent>
        <artifactId>saplication</artifactId>
        <groupId>net.benteler.apps.saplication</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </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-actuator</artifactId>
        </dependency>

        <!-- Spring Boot Logging with Log4J-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
                <!-- Data handling -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
        </dependency>


        <!-- Database Driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
        </dependency>

        <!-- Unit testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- JSON handling-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

        <!-- Templating - deliver JSP-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>


    </dependencies>

    <build>
        <finalName>saplication-snapshot</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在我的application.properties文件中,我添加了以下内容:
spring.view.prefix = /templates/
spring.view.suffix = .html

我的Application.java文件看起来像这样:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.out.println("Hallo");
        SpringApplication.run(SaplicationApplication.class, args);
    }
}

我的控制器

@Controller
public class MainController {

    private static final Logger logger = LogManager.getLogger(MainController.class.getName());

    @RequestMapping("/test")
    public String index(ModelAndView modelAndView) {
        logger.info("/test");
        logger.debug("Hello world - debug log");
        logger.info("Hello world - info log");
        logger.warn("Hello world - warn log");
        logger.error("Hello world - error log");
        return "index";
    }

}

当我启动应用程序时,映射正在工作。
Mapped "{[/test]}" onto public java.lang.String saplication.controller.MainController.index(org.springframework.web.servlet.ModelAndView)

并且日志消息被显示出来

[INFO ] 2015-10-06 08:41:37.477 [http-nio-8080-exec-1] MainController - /test
[INFO ] 2015-10-06 08:41:37.478 [http-nio-8080-exec-1] MainController - Hello world - info log
[WARN ] 2015-10-06 08:41:37.478 [http-nio-8080-exec-1] MainController - Hello world - warn log
[ERROR] 2015-10-06 08:41:37.478 [http-nio-8080-exec-1] MainController - Hello world - error log

但是当我输入http://localhost:8080/test时,我只能看到信息。
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Oct 06 08:41:37 CEST 2015
There was an unexpected error (type=Not Found, status=404).
No message available

乍一看似乎一切正常,但我遇到的问题是Spring找不到我的index.html模板。

我真的不知道我的配置有什么问题!所以我需要你的帮助。


你可以尝试使用http://localhost:8080/<yourAppName>/test和Maven的快照名称http://localhost:8080/<yourAppName-0.0.1-SNAPSHOT>/test进行测试吗? - Patrick
你的应用程序在GitHub或其他地方吗? - Nikolay Rusev
似乎 JSP 页面不存在(index.jsp)。所以,你能否尝试创建它并再次检查? - kucing_terbang
你的项目没有遵循默认的Maven项目结构。如果你是Maven的新手,首先采用单模块方法开始。在你理解了Maven的基础知识之后,编写一些简单的Spring Boot应用程序 - 然后尝试使用多模块。 - Maciej Walkowiak
3个回答

1
解决方法:将您的IDE的工作目录更改为您的模块,因为Spring Boot嵌入Tomcat使用顶级路径加上您定义的路径来路由Web根目录。

0
将您的资源放在 web 模块中的 src/main/resources 文件夹中。这是资源的默认 Maven 结构。

0
请在您的 Application.java 文件中进行以下更改。
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }

    }

SpringBootServletInitializer

这是运行Web应用程序所必需的。它将启用Servlet容器并将您的应用程序绑定到Servlet容器。

更新:

要运行您的主模块(Web模块),您必须进入该目录,然后使用mvn spring-boot:run命令运行您的项目。

示例:1. [me@linuxbox myApp] $ cd /web 2. [me@linuxbox web] $ mvn spring-boot:run


只有在构建war并将其部署到独立容器时才需要这样做。如果您遵循推荐的和默认的方法使用嵌入式容器,则不需要这样做。 - Andy Wilkinson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接