我对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>
演示模块中的 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/用户
这将显示 404 错误。有人可以帮我解决这个问题吗?
谢谢