此问题的正确解决方案仍未在互联网线程中提供。 我在泽西RESTFul Web服务的eclipse中有一个maven项目。因此,图书馆(21号)下面需要的运动衫被复制到项目中 - > WEB-INF - > lib文件夹和项目构建路径。
aopalliance-repackaged-2.4.0-b34.jar
asm-debug-all-5.0.4.jar
hk2-api-2.4.0-b34.jar
hk2-locator-2.4.0-b34.jar
hk2-utils-2.4.0-b34.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.4.0-b34.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar jersey-client.jar
jersey-common.jar
jersey-container-servlet.jar
jersey-container-servlet-core.jar
jersey-guava-2.23.1.jar
jersey-media-jaxb.jar jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
当我将此项目导出为WAR文件并部署在Tomcat中时,可以访问该服务。但实际要求是在WebLogic服务器中运行此服务,其中相同的WAR文件不起作用。因此,我必须使用maven创建WAR文件,而不是仅导出为WAR文件。
以下是我的步骤。
在Eclipse中 - >项目 - >右键单击 - >作为Maven构建运行 - >全新安装 - >运行
它仍然抛出以下编译错误。
[错误]编译错误: [INFO] ----------------------------------------------- -------------- [错误] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[6,19]包javax.ws.rs不存在 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[7,19]包javax.ws.rs不存在 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[8,19]包javax.ws.rs不存在 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[9,24]包javax.ws.rs.core不存在 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[10,24]包javax.ws.rs.core不存在 [错误] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[11,33]包javax.ws.rs.core.Response不存在 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[23,2]无法找到符号 符号:类路径 [ERROR] /Users/karthik/Documents/workspace/Apple_DownloadFileWS/src/com/apple/downloadws/DownloadService.java:[31,12]无法找到符号 符号:类响应
这是我的pom.xml:
<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>Apple_DownloadFileWS</groupId>
<artifactId>Apple_DownloadFileWS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Apple_DownloadFileWS</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles.repackaged/jersey-guava -->
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-guava</artifactId>
<version>2.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-jaxb -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Apple_DownloadFileWS</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.apple.downloadws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
服务类:
package com.apple.downloadws;
import java.io.File;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
// Plain old Java Object it does not extend as class or implements
// an interface
// The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.
// The browser requests per default the HTML MIME type.
//Sets the path to base URL + /hello
@Path("/downloadservice")
public class DownloadService {
// This method is called to send the output txt file to download...
private static final String FILE_PATH = "/ngs/app/alect/test";
@GET
@Path("/txt")
@Produces("text/plain")
public Response getTextFile() {
File file = new File(FILE_PATH);
Logger.getLogger("!!!!!!!!!!!");
System.out.println("!!!!!!!!!!");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition","attachment; filename=\"sample.txt\"");
return response.build();
}
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
我可以知道应该进入maven pom.xml依赖项吗?我用谷歌搜索并添加上述依赖项仍然得到相同的错误。我在这里缺少任何依赖吗?
答案 0 :(得分:0)
您已在dependencyManagement
部分列出了所有依赖项,而不是pom.xml的dependencies
部分。 dependencyManagement
用于管理依赖项的版本和范围,对于瞬态依赖项或确保跨多个模块项目的一致性特别有用,但它实际上并未将依赖项放在类路径上。要将依赖项添加到类路径中,您需要在dependencies
部分中列出它们。你的pom.xml目前的方式,你的构建完全没有依赖。