Home > Java > javaTutorial > How to Solve CORS Issues in JAX-RS with Jersey?

How to Solve CORS Issues in JAX-RS with Jersey?

DDD
Release: 2024-12-19 15:34:14
Original
829 people have browsed it

How to Solve CORS Issues in JAX-RS with Jersey?

Handling CORS with JAX-RS Using Jersey

Problem: You're encountering a CORS issue while handling requests with JAX-RS and Jersey. Specifically, the server is not setting the necessary CORS headers, leading to the error "No 'Access-Control-Allow-Origin' header is present on the requested resource."

Solution: To handle CORS in JAX-RS with Jersey, you need to implement a ContainerResponseFilter. The difference in implementation depends on whether you're using Jersey 1.x or 2.x.

Jersey 2.x:

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

@Provider
public class CORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
        MultivaluedMap<String, Object> headers = responseContext.getHeaders();
        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Headers", "CSRF-Token, X-Requested-By, Authorization, Content-Type");
        headers.add("Access-Control-Allow-Credentials", "true");
        headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
    }
}
Copy after login

Jersey 1.x:

import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

@Provider
public class CORSFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerResponse response) {
        response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHttpHeaders().add("Access-Control-Allow-Headers", "CSRF-Token, X-Requested-By, Authorization, Content-Type");
        response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
        response.getHttpHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");

        return response;
    }
}
Copy after login

Additional Notes:

  • Ensure that the CORS filter is registered with the ResourceConfig object.
  • For better handling, consider using a more robust implementation that differentiates between simple and preflight CORS requests.
  • Refer to the CORSFilter implementation in RESTeasy for a more comprehensive example.

The above is the detailed content of How to Solve CORS Issues in JAX-RS with Jersey?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template