수색…


교차점 요청을위한 Java Jersey CORS 필터

@Provider
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", "*"); //Allow Access from everywhere   
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");            
        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
    }
}

Access-Control-Allow-Origin은 OPTIONS 응답에서만 유용합니다.

자바 저지 구성

이 예제는 Jersey를 RESTful API를위한 JAX-RS 구현 프레임 워크로 사용할 수 있도록 Jersey를 구성하는 방법을 보여줍니다.

Apache Maven 을 이미 설치했다고 가정하고, 다음 단계에 따라 Jersey를 설정하십시오.

  1. 메이븐 웹 프로젝트 구조를 만들고 터미널 (창)에서 다음 명령을 실행하십시오.

mvn archetype : 생성 -DgroupId = com.stackoverflow.rest -DartifactId = jersey-ws-demo -DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false

참고 : Eclipse를 지원하려면 Maven 명령을 사용하십시오. mvn eclipse : eclipse -Dwtpversion = 2.0

  1. pom.xml에서 maven 프로젝트를 생성 한 폴더로 이동하여 필요한 의존성을 추가하십시오.
<dependencies>
    <!-- Jersey 2.22.2 -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <!-- JSON/POJO support -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

<properties>
    <jersey.version>2.22.2</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
  1. Web.xml에 다음 코드를 추가하십시오.
<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>
        <!-- Service or resources to be placed in the following package --> 
        <param-value>com.stackoverflow.service</param-value>
    </init-param>
   
    <!-- Application configuration, used for registering resources like filters  -->
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.stackoverflow.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Url mapping, usage-http://domainname:port/appname/api/ -->
 <servlet-mapping>
     <servlet-name>jersey-serlvet</servlet-name>
     <url-pattern>/api/*</url-pattern>
 </servlet-mapping>
  1. ApplicationConfig 클래스
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(OtherStuffIfNeeded.class);
    }
}

또한 web.xml을 사용 하지 @ApplicationPath("/api") 려면 ApplicationConfig 을 제거하고 ApplicationConfig 클래스 위에 @ApplicationPath("/api") 를 추가 @ApplicationPath("/api") 합니다.

@ApplicationPath("/api")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        // this call has the same effect as
        // jersey.config.server.provider.packages
        // in the web.xml: it scans that packages for resources and providers. 
        packages("com.stackoverflow.service");
    }
}
  1. Maven 프로젝트를 빌드하고 배포하십시오.
  2. Jersey의 jar를 사용하도록 Java RESTful webservice (JAX-RS) 클래스를 설정할 수 있습니다.


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow