如何使用spring boot创建多模块maven项目
我对spring boot和maven真的很陌生。我尝试使用maven创建一个多模块spring引导项目。当我创建它并试图通过url访问我的控制器端点时,它将显示404错误 我已经尝试了很多来解决这个问题,并在网上的文档中搜索,但我失败了。谁能帮我解决这个问题 在我的项目中有两个模块叫做demo和activityops 父pom.xml如何使用spring boot创建多模块maven项目,spring,spring-boot,maven-3,spring-rest,Spring,Spring Boot,Maven 3,Spring Rest,我对spring boot和maven真的很陌生。我尝试使用maven创建一个多模块spring引导项目。当我创建它并试图通过url访问我的控制器端点时,它将显示404错误 我已经尝试了很多来解决这个问题,并在网上的文档中搜索,但我失败了。谁能帮我解决这个问题 在我的项目中有两个模块叫做demo和activityops 父pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apa
<?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>
我已键入以下url以访问控制器方法。 这将显示404错误。谁能帮我解决这个问题
谢谢您的项目目前正在进行很多工作,但最大的问题是您的
演示
项目不依赖于其他模块
此外,该模块在根包中定义控制器(不要这样做!)。您的Spring Boot应用程序位于com.example
。默认情况下,Spring Boot将扫描Spring Boot应用程序所在的包及其所有子包TestController
不在com中。示例
因此找不到它。这就是你得到404的原因
还有其他问题,我已经演示了如何让这个示例项目工作。有关更多详细信息,请查看提交消息 您的问题中没有任何内容与您的应用程序是一个多模块项目这一事实真正相关。如果你想要一个快速有效的答案,最简单的方法就是分享这个项目(比如在github上)并提供链接。嗨,Stephane,我已经将这个项目添加到git hub中,你可以通过下面的链接访问它。谢谢你的支持。。。。!嗨,Stéphane,我在使用mvn spring boot:run命令运行这个应用程序时遇到了一个问题。然后显示java.lang.ClassNotFoundException:com.example.DemoApplication异常。我已经试着自己解决这个问题,但仍然无法做到。你能告诉我如何解决这个问题吗。。。
<?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>
<?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;
}
}