GithubHelp home page GithubHelp logo

shravangottimukula / graphgl-java-cors-header Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidctj/graphgl-java-cors-header

0.0 1.0 0.0 58 KB

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

Java 49.86% HTML 50.14%

graphgl-java-cors-header's Introduction

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

graphgl-java-cors-header's People

Contributors

davidctj avatar

Watchers

Shravan Gottimukula avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.