title: How To Receive JSON Data In Cross Origin Resource Sharing (CORS) with Spring REST
body: <p>I am trying to <strong>send / Receive</strong> <strong>JSON</strong> data  <strong>from / to</strong>  my Controller Class in my Project.</p>
  
  <p>The <strong>Controller Class</strong> Of My Project Is as Follows :</p>
  
  <pre><code>@RequestMapping(value = "/dummy/",method = RequestMethod.POST,headers="Accept=application/json")
  public Response getDummyJSON() {
  
      /*  Method 2: Getting the Dummy JSON Data From file and sending it as an JSON Object  */
  
      JSONParser parser = new JSONParser();
      JSONObject jsonObject = null;
      try {
  
          Object obj = parser.parse(new FileReader("d:\\JsonStruc.json"));
          jsonObject = (JSONObject) obj;
  
      } catch (Exception e) {
          System.out.println("Error Parsing: - ");
      }   
  
  
          return Response.status(200).entity(jsonObject).build();             
  
  }
  
  @RequestMapping(value = "/insert/dummy/",method= RequestMethod.POST,headers="Accept=application/json")
  public Response setDummyJSON(@RequestBody JSONObject inputjsonObj){
  
      /*Step 1: Display The JSON data sent by AngularJS */ 
      System.out.println("Data Received:"+ inputjsonObj.toString());
      JSONParser jsonparser = new JSONParser();
      ContainerFactory containerFactory = new ContainerFactory(){
          public List creatArrayContainer() {
              return new LinkedList();
          }
  
          public Map createObjectContainer() {
              return new LinkedHashMap();
          }
  
      };
  
      Map obj = (Map)inputjsonObj;
      Iterator iter = obj.entrySet().iterator();
      while(iter.hasNext()){
          Map.Entry entry = (Map.Entry)iter.next();
          System.out.println(entry.getKey() + "=&gt;" + entry.getValue());
  
      }
  
      /* Step 2: send the next dummy JSON file */
      JSONParser parser = new JSONParser();
      JSONObject jsonObject1 = null;
      try {
  
          Object obj1 = parser.parse(new FileReader("d:\\JsonStruc1.json"));
          jsonObject1 = (JSONObject) obj1;
  
      } catch (Exception e) {
          System.out.println("Error Parsing: - ");
      }   
      return Response.status(200).entity(jsonObject1).build();          
   }  
  </code></pre>
  
  <p>The <strong>CORS</strong> class of my Project is as follows :</p>
  
  <pre><code>public class CorsFilter extends OncePerRequestFilter{
  
   private Properties prop = new Properties();
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
              throws ServletException, IOException {
  
  
           System.out.println("Inside Cross Origin Resource Sharing Filter Class");
           System.out.println(request.getContentType());
           System.out.println(request.getContentLength());
  
           response.setHeader("Access-Control-Allow-Origin", "*");
           response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD, PUT");
           response.setHeader("Access-Control-Max-Age", "3600");
           response.setHeader("Access-Control-Allow-Credentials", "true");
           response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
  
           filterChain.doFilter(request, response);
      }
  
  }
  </code></pre>
  
  <p>There is <em>no Problem</em> regarding sending Dummy JSON data through the URL (<em>/service/question/dummy/</em>) But
  when I am trying to get The JSON data (<em>/service/question/insert/dummy/</em>) and send the next JSON object
  through the method <em>setDummyJSON(....)</em> then I am having trouble. The AngularJS (Client) side is showing the following <strong>exception message</strong> :</p>
  
  <blockquote>
    <p>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at   <strong>/FetchQuestions/service/question/insert/dummy/</strong>. This can be fixed by moving the resource to the same domain or enabling CORS.</p>
  </blockquote>
  
  <p>On invoking the URL (<em>/service/question/insert/dummy/</em>) from localhost with a JSON String as Content Body the following error message is thrown :</p>
  
  <blockquote>
    <p>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</p>
  </blockquote>
  
  <p>What am I doing Wrong ? Any Help will be appreciated. Thanks.</p>