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

时间:2017-06-29 02:02:17

标签: rest maven jersey pom.xml web.xml

我正在构建用于使用maven从mysql中选择数据的restful应用程序。我对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>
&#13;
&#13;
&#13;

我是多个包,如下所示:

enter image description here

问题是我是否需要为每个包添加另一个groupid和artifactid。当我访问在com.tutorialacademy.rest包类下编码为@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>

1 个答案:

答案 0 :(得分:1)

<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可以注册这些类。您只列出了您说的有效的一个包。您可以添加多于逗号分隔的包

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

虽然通常的做法是为您的项目提供顶级包。例如

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

以递归方式扫描您在param-value中列出的包。所以你可以在这种情况下使用com.foo,然后所有子包也会被扫描。