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)