在Jersey REST API中为CORS配置Web.xml

时间:2018-02-13 05:15:35

标签: java rest jersey-2.0

我已创建了休息网络服务,并希望为CORS启用。 我已将以下代码添加到我的API代码

package RestClient;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;

public class CORSResponseFilter
implements ContainerResponseFilter {

    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException {

        MultivaluedMap<String, Object> headers = responseContext.getHeaders();

        headers.add("Access-Control-Allow-Origin", "*");        
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");          
        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
    }



}

为了可访问性,我需要在web.xml文件中设置上面的类。所以这是我的web.xml

<?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" version="3.1">
  <display-name>GenerateToken</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>JAVA WS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>RestClient</param-value>
</init-param>
<init-param>
    <param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
    <param-value>RestClient.CORSResponseFilter</param-value>
</init-param>       
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>JAVA WS</servlet-name>
    <url-pattern>/Test/*</url-pattern>
</servlet-mapping>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>GenerateToken</web-resource-name>
        <url-pattern>/Test/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
</web-app>

我只是想知道,我是否为CORS配置了正确的web.xml文件?在我的本地工作,但在我为每个人部署它之前,只是想确保它是否正确。

有人可以就此提出建议吗?

1 个答案:

答案 0 :(得分:3)

你不需要这个(它甚至不正确)

<init-param>
    <param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
    <param-value>RestClient.CORSResponseFilter</param-value>
</init-param> 

只需在您的过滤器类之上添加@Provider即可。以下内容将扫描RestClient所有@Path@Provider类的包,并为您注册。

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>RestClient</param-value>
</init-param>