多个包是否应该在 POM 配置中有相应的 groupid 和 artifactid

does multiple packages should have corresponding groupid and artifactid in POM configuration

我正在构建 restful 应用程序以使用 Maven 从 mysql 中选择数据。我对 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>
 <groupId>com.tutorialacademy.rest</groupId>
 <artifactId>helloworld</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>helloworld Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <repositories>
  <repository>
   <id>maven2-repository.java.net</id>
   <name>Java.net Repository for Maven</name>
   <url>http://download.java.net/maven/2/</url>
   <layout>default</layout>
  </repository>
 </repositories>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.9</version>
  </dependency>
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.3.3</version>
  </dependency>
  <dependency>
   <groupId>com.googlecode.json-simple</groupId>
   <artifactId>json-simple</artifactId>
   <version>1.1</version>
  </dependency>
  <dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20080701</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.6.2</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>

 <build>
  <sourceDirectory>src/main/java</sourceDirectory>

  <plugins>

   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>

   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>

  </plugins>

 </build>

</project>

虽然我是多个包如下图:

问题是我需要为每个包添加另一个 groupid 和 artifactid 吗?当我访问 com.tutorialacademy.rest 包的 class 下编码为 @path 的路径 http://localhost:8080/helloworld/rest/exportfile/json 时,它确实有效:

但是当我访问 webService 包下的路径 http://localhost:8080/helloworld/rest/helloService/hello/ 时。这给了我错误:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

我的web.xml如下:

<servlet>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.tutorialacademy.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>helloworld</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.tutorialacademy.rest</param-value>
</init-param>

param-value 列出了应该扫描 类 并用 @Path 注释的包,以便 Jersey 可以注册那些 类。你只列出了一个你说有效的包。您可以添加 more than on package 以逗号分隔

<param-value>
    com.tutorialacademy.rest,
    webService
</param-value>

虽然为您的项目设置顶级包是常见的做法。例如

com.foo
com.foo.dao
com.foo.model
com.foo.resources

您在参数值中列出的包将被递归扫描。所以在这种情况下你可以只使用 com.foo,然后所有子包也会被扫描。