Skip to content

# GarphQL Java server with CORS header enabled, gradle 4 build

Notifications You must be signed in to change notification settings

davidctj/graphgl-java-cors-header

Repository files navigation

GarphQL Java server with CORS enabled

  • based ongraphql-java-tutorial
  • add filters for Jetty server to allow Cross-Origin Resource Sharing(CORS) header.
  • jdk 1.8+
  • built with gradle 4.2.1+

How to run

$ ./gradlew clean jettyRun

Then visit localhost:8080 and write your graphql query there.

CORS

  1. create filter for web server and set the HTTP CORS header.
public class CORSFilter implements Filter {

    
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
      
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        
        // Authorize (allow) all domains to consume the content
        ((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Origin", "*");
        ((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Methods","GET, OPTIONS, HEAD, PUT, POST");
        ((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Headers", "Content-Type, token, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        
        // For HTTP OPTIONS verb/method reply with ACCEPTED status code -- per CORS handshake
        if (request.getMethod().equals("OPTIONS")) {
            resp.setStatus(HttpServletResponse.SC_ACCEPTED);
            return;
        }
        
        // pass the request along the filter chain
        chain.doFilter(request, servletResponse);
  }
    
}
  1. add your filter in your WEB-INF/web.xml file

About

# GarphQL Java server with CORS header enabled, gradle 4 build

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published