我正在使用Jersey進行Java中的RESTFul Web服務,我試圖在Java方法中獲取調用者IP地址,而不傳遞參數。嘗試獲取RESTful WS中的WebServiceContext時發生注入錯誤使用Java Jersey
我嘗試了幾個其他職位與StackOverflow中的類似問題,但他們都沒有工作。到目前爲止,我有一個注射錯誤,我不知道如何解決它。
這段代碼需要在一個服務器中的Tomcat 8容器內運行,另一個服務器中運行JBoss。但是,當我的項目部署到任何人,同樣的錯誤,同時啓動容器拋出:
重度:具有資源和/或供應商類已檢測到以下錯誤和警告: 嚴重:缺少依賴於現場:javax.xml.ws.WebServiceContext com.adamiworks.restfultutorial.JsonService.webServiceContex噸
這個錯誤也從Eclipse中運行到Tomcat 8時出現
任何人都可以點我在哪裏,我錯誤?
我有這個類:
package com.adamiworks.restfultutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@Path("/json")
public class JsonService {
@Context
WebServiceContext webServiceContext;
@GET
@Path("/{param}")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getMsg(@PathParam("param") String msg) {
HttpServletRequest request = this.getHttpContext();
String clientIpAddress = request.getHeader("X-FORWARDED-FOR");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse("1984-01-08");
} catch (ParseException e) {
e.printStackTrace();
}
Cliente c = new Cliente();
c.setNome("Anyone");
c.setDataNascimento(d);
JsonObject o = new JsonObject(msg, c);
o.setText("IP=" + clientIpAddress);
return o;
}
private HttpServletRequest getHttpContext() {
MessageContext mc = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
return request;
}
}
有了這個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>com.adamiworks</groupId>
<artifactId>restful-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>adamiworks restful-tutorial</name>
<description>restful-tutorial ftw</description>
<properties>
<jdk.version>1.8</jdk.version>
<jersey.version>1.19.3</jersey.version>
<javax.ws.rs.version>1.1.1</javax.ws.rs.version>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>${javax.ws.rs.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>