Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to enable Cross-Origin Requests for a RESTful web service.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 428
    Comment on it

    The RESTful web service need to include CORS access control headers in its response, there we have to add a @CrossOrigin annotation to the handler method. It enables cross-origin requests only for the particular method. It allows all the HTTP methods and headers that are specified in @RequestMapping, origins and a maxAge of 30 min, by default.

    Example:

    @RestController
    @RequestMapping("/documents")
    @CrossOrigin
    @RequestMapping(value = "/uploadfile2", method = RequestMethod.POST)
    @ResponseBody
    public  void multipleUpload( @RequestParam("name") String name,
                @RequestParam("qqfile") MultipartFile file, JsonResponse json, HttpServletRequest request, HttpServletResponse response ) throws IllegalStateException, IOException, JSONException 
    {
            System.out.println("inside mult***************"+file.getSize());
    }
    

    We can also customize these attributes, i.e, origins, header and HTTP methods by specifying their values.

    @CrossOrigin(origins = "http://localhost:9000")
    

    Here only http://localhost:9000 is allowed to send cross-origin request.

    @CrossOrigin(maxAge = 3600)
    

    Bydefault, all Httpmethods GET, POSTand HEAD are allowed.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: