簡體   English   中英

嘗試使用Java Jersey在RESTful WS中獲取WebServiceContext時出現注入錯誤

[英]Injection error while trying to get WebServiceContext in RESTful WS with Java Jersey

我正在將Jersey用於Java中的RESTFul Web服務,並且試圖在Java方法中獲取調用方IP地址,而沒有作為參數傳遞。

我在StackOverflow中嘗試了其他類似問題的其他帖子,但都沒有用。 到目前為止,我遇到了注入錯誤,而且我不知道如何解決。

這段代碼需要在一個服務器的Tomcat 8容器中運行,而在另一服務器的JBoss中運行。 但是,當我將項目部署到其中的任何一個時,啟動容器時都會引發相同的錯誤:

嚴重:已使用資源和/或提供程序類檢測到以下錯誤和警告:嚴重:字段缺少依賴項:javax.xml.ws.WebServiceContext com.adamiworks.restfultutorial.JsonService.webServiceContex t

從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>

感謝很多人,包括

HRITUPON

大家在這里

每個人都在這里

我終於做到了!

事實證明:

  1. 上下文注入在Jersey 1.xx中不可用-需要升級到版本2+。
  2. Glassfish現在維護從2.xx開始的澤西版,因此pom.xml和web.xml有很多不同;
  3. WebServiceContext和MessageContext是基於SOAP的,在Java RESTFul Web Service中沒有意義,因為它沒有接收到特定的SOAP標頭。

對於那些同樣遇到錯誤的用戶,請注意以下更改:

pom.xml :更改為Jersey 2.24.1

<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>2.24.1</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>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

web.xml更改的Servlet類

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>restful-tutorial</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>jersey-serlvet</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.adamiworks.restfultutorial</param-value>
        </init-param>
        <init-param>
            <param-name>org.glassfish.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

帶有服務的Java類 :請忽略其他不在上下文中的類。

package com.adamiworks.restfultutorial;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
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;

@Path("/json")
public class JsonService {

    @Context
    private ServletContext servletContext;

    @Context
    private HttpServletRequest request;

    @GET
    @Path("/{param}")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonObject getMsg(@PathParam("param") String msg) {

        // This is the method that get the correct IP address
        String clientIpAddress = getClientIpAddr();

        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;
    }

    public String getClientIpAddr() {
        String ip = request.getHeader("X-FORWARDED-FOR");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip.equals("0:0:0:0:0:0:0:1")) {
            InetAddress localip;
            try {
                localip = java.net.InetAddress.getLocalHost();
                ip = localip.getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        return ip;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM