【问题标题】:How to create a multi-module maven project with spring boot如何使用spring boot创建多模块maven项目
【发布时间】:2015-12-27 09:58:03
【问题描述】:

我对 spring boot 和 maven 真的很陌生。我尝试使用 maven 创建一个多模块 Spring Boot 项目。当我创建它并尝试通过 url 访问我的控制器端点时,它将显示 404 错误。

我已经尝试了很多解决这个问题并在在线文档中搜索但我失败了。谁能帮我解决这个问题。

在我的项目中有两个模块,分别称为 demo 和 activityops。

父 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>org.multimodule</groupId>
<artifactId>multimodule</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
    <module>activityops</module>
    <module>demo</module>
</modules>

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

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <start-class>com.example.DemoApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


  </project>

activityops 模块中的 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>multimodule</artifactId>
    <groupId>org.multimodule</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>activityops</artifactId>

demo模块中的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>multimodule</artifactId>
    <groupId>org.multimodule</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>


</project>

我的控制器类在下面

import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.ws.rs.core.MediaType;


@org.springframework.web.bind.annotation.RestController
@RequestMapping("/user")
public class TestController {
private static org.slf4j.Logger LOGGER =     LoggerFactory.getLogger(TestController.class);

    @RequestMapping(method = RequestMethod.GET, produces =  {MediaType.APPLICATION_JSON})
    public User getSingleUser() {

        User user = new User();
        user.setId(11l);
        user.setFirstName("Shane");
        user.setLastName("John");
        return user;
    }
}

我输入了以下 url 来访问控制器方法。 http://localhost:8080/user

这将显示 404 错误。谁能帮我解决这个问题?

谢谢

【问题讨论】:

  • 您的问题中没有任何内容与您的应用程序是一个多模块项目这一事实真正相关。如果您想要一个快速有效的答案,最简单的方法是分享项目(例如在 github 上)并提供链接。
  • 嗨 Stephane 我已将项目添加到 git hub 中,您可以通过以下链接访问它。谢谢你的支持....! github.com/dilan99x/mutimodule

标签: spring spring-boot maven-3 spring-rest


【解决方案1】:

您的项目现在有很多事情要做,但最大的问题是您的demo 项目不依赖于其他模块

此外,该模块在根包中定义了您的控制器(不要那样做!)。您的 Spring Boot 应用程序位于 com.example。默认情况下,Spring Boot 会扫描你的 Spring Boot 应用程序所在的包及其所有子包。 TestController 不在 com.example 中,所以找不到。这就是你得到 404 的原因。

还有其他问题,我有submitted a pull request,它演示了您可以做些什么来使这个示例项目正常工作。检查提交消息以获取更多详细信息。

【讨论】:

  • 嗨 Stéphane 我在使用 mvn spring-boot:run 命令运行此应用程序时遇到问题。然后它显示 java.lang.ClassNotFoundException: com.example.DemoApplication 异常。我试图自己解决这个问题,但我仍然无法做到。你能告诉我如何解决这个问题...
猜你喜欢
  • 2020-04-01
  • 2015-10-10
  • 2014-06-02
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2016-09-13
  • 2017-02-02
相关资源
最近更新 更多