How do I activate a CORS Filter for Cross Domain POST Requests in Maven?

I made a React Application deployed to a separate Google App Engine, which sends a POST Request to fetch some data from a Webapp, and I’m trying to parse the data within my React Application. I was able to fetch the correct data from PostMan, but the same request kept giving me a “No ‘Access-Control-Allow-Origin’ Header is present on the requested resource” Error.
I’m currently receiving the PostRequest from the Bigger Webapp like this:

@POST
@Path("/initializer")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String getOrCreateSeller(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

    this.shopifyStoreName = body.get("id").toString();
    long hashedName = hashStoreName();
    SellerId sellerId = new SellerId(hashedName);
    try {
        Seller seller = sellerService.findSellerById(sellerId);
        getStore(seller);
        String json = getResponseJson();
        return Response.ok(json, MediaType.APPLICATION_JSON).build();
    } catch (NotFoundException e) {
        Seller seller = new Seller(sellerId.toDatastoreId(), 
     this.shopifyStoreName, 3L);
        datastoreRepository.transact(() -> {
            sellerValidator.validate(seller);
            sellerRepository.save(seller);
        });
        createStore(seller);
        String json = getResponseJson();
        Response.ResponseBuilder r = Response.ok(json, MediaType.APPLICATION_JSON);
        return r.build();
    }
}

It turns out that in the bigger webapp I’m fetching data from contains a “CORSFilter” Class that looks like this:

public class CORSFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
        ServletException {
    HttpServletResponse httpResp = (HttpServletResponse) resp;
    HttpServletRequest httpReq = (HttpServletRequest) req;

    httpResp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
    httpResp.setHeader("Access-Control-Allow-Origin", "*");
    if (httpReq.getMethod().equalsIgnoreCase("OPTIONS")) {
        httpResp.setHeader("Access-Control-Allow-Headers",
                httpReq.getHeader("Access-Control-Request-Headers"));
    }
    chain.doFilter(req, resp);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

@Override
public void destroy() {
}

}

Hence, I feel like I should use this class to enable CORs, but I am not sure how. Please help!

As your server-side code seems to be using JAX-RS, a way to add CORS Headers to a JAX-RS application (following the same rules of that CORSFilter of yours) is just to add this class to your app:

@Provider
@Priority(Priorities.HEADER_DECORATOR)
public class AccessControlResponseFilter implements ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        final MultivaluedMap<String,Object> headers = responseContext.getHeaders();

        headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
        headers.add("Access-Control-Allow-Origin", "*");
        if (requestContext.getMethod().equalsIgnoreCase("OPTIONS")) {
            headers.add("Access-Control-Allow-Headers", requestContext.getHeaderString("Access-Control-Request-Headers"));
        }
    }
}

Alternatively, if you have that CORSFilter class available to your project, you could add it as a servlet filter to your web.xml:

<filter>
    <filter-name>CORSFilter</filter-name>
    <filter-class>com.mycompany.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Pick one of the two solutions, not both.

You can use @CrossOrigin annotation in controller class. Can get more information here – crossorigin