This tutorial shows how to create Source Application which will be used to demonstrate CORS functionality.
Source Application will run on Port 8080 and it will contain HTML with Java Script calling Destination Application.
Source Application created in this tutorial will remain the same across subsequent tutorials.
This is because all CORS related changes only need to be done in Destination Application.
It is Destination Application that needs to allow CORS access to its resources from other Origins/Applications.
Application Schema [Results]
Spring Boot Starters
Create Project: springboot_cors_source (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create HTML File: Test.html (inside directory resources/templates)
MyController.java
package com.ivoronline.springboot_cors_source.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
@GetMapping("/Hello")
public String hello() {
return "Test";
}
}
Test.html
<title>Test.html</title>
<p>Hello from Test.html</p>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'http://www.ivoronline.com/Images/Banners/Banner_1.png');
request.send(); //This line throws CORS Error
</script>