向控制器发出http GET时发生404错误

时间:2018-06-14 11:13:25

标签: maven http spring-mvc

我在项目中为我的API服务提供了一个单独的模块,它是新包含的。但我无法向此模块中的控制器发出HTTP请求。

项目主pom文件:

<?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>pms.quiz</groupId>
<artifactId>pms-quiz</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
    <module>orm</module>
    <module>core</module>
    <module>rest</module>
    <module>operatorServices</module>
    <module>api</module>
</modules>
<dependencies>
</dependencies>
<name>pms-quiz</name>

核心模块的pom文件:

<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>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>core</name>
<packaging>jar</packaging>

<parent>
    <groupId>pms.quiz</groupId>
    <artifactId>pms-quiz</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<properties>
    <jdk.version>1.7</jdk.version>
    <spring.version>3.2.2.RELEASE</spring.version>
    <jackson.version>1.9.10</jackson.version>
</properties>

<dependencies>

</dependencies>

<build>
    <finalName>core</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

api模块的pom文件:

<?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>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<name>api</name>
<packaging>jar</packaging>


<parent>
    <artifactId>pms-quiz</artifactId>
    <groupId>pms.quiz</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<properties>
    <jdk.version>1.7</jdk.version>
    <spring.version>3.2.2.RELEASE</spring.version>
    <jackson.version>1.9.10</jackson.version>
</properties>


<dependencies>
    <dependency>
        <groupId>hms.quiz</groupId>
        <artifactId>core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
    </dependency>
</dependencies>

<build>
    <finalName>api</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
     </plugins>
   </build>
 </project>

构建为WAR文件的模块,包括与其他模块的依赖关系:

<?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>
<artifactId>rest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>rest</name>
<packaging>war</packaging>

<parent>
    <groupId>hms.quiz</groupId>
    <artifactId>hms-quiz</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<properties>
    <spring.version>3.2.2.RELEASE</spring.version>
    <jackson.version>1.9.10</jackson.version>
    <jdk.version>1.7</jdk.version>
</properties>

<dependencies>
    <dependency>
        <groupId>hms.quiz</groupId>
        <artifactId>operatorServices</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>hms.quiz</groupId>
        <artifactId>api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <warName>quiz-rest</warName>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${artifactId}</finalName>
   </build>

 </project>

api模块中的控制器类:

@Controller
@RequestMapping("/user")
public class UserController {

 @Autowired
UserService userService;
   @RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<User>> listAllUsers() {
    List<User> users = userService.getAllActiveUsers();
    if (users.isEmpty()) {
        return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
    }
    return new ResponseEntity<List<User>>(users, HttpStatus.OK);
    }

  }

项目建设很好。我可以向这些其他模块中的控制器发出HTTP请求但是这个api模块。我总是得到404错误。

我做的GET请求:

http://127.0.0.1:8080/quiz-rest/user

这可能是什么原因?哪里可能出错? 提前致谢。

0 个答案:

没有答案