From d84e1bd4cf6044973f0240374f26519d4b2bd919 Mon Sep 17 00:00:00 2001 From: Erik Pragt Date: Mon, 8 Jul 2019 17:34:55 -0500 Subject: [PATCH] BAEL-3036 Update Cors with Spring article and create the code --- .../main/java/com/baeldung/cors/Account.java | 9 +++++++ .../com/baeldung/cors/AccountController.java | 24 ++++++++++++++++++ .../java/com/baeldung/cors/WebConfig.java | 16 ++++++++++++ .../main/webapp/WEB-INF/spring-web-config.xml | 25 +++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 spring-rest-simple/src/main/java/com/baeldung/cors/Account.java create mode 100644 spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java create mode 100644 spring-rest-simple/src/main/java/com/baeldung/cors/WebConfig.java create mode 100644 spring-rest-simple/src/main/webapp/WEB-INF/spring-web-config.xml diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/Account.java b/spring-rest-simple/src/main/java/com/baeldung/cors/Account.java new file mode 100644 index 0000000000..dc6a541a46 --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/cors/Account.java @@ -0,0 +1,9 @@ +package com.baeldung.cors; + +public class Account { + private Long id; + + public Account(Long id) { + this.id = id; + } +} diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java b/spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java new file mode 100644 index 0000000000..c387eb2121 --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java @@ -0,0 +1,24 @@ +package com.baeldung.cors; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@CrossOrigin(maxAge = 3600) +@RestController +@RequestMapping("/account") +public class AccountController { + + @CrossOrigin("http://example.com") + @RequestMapping("/{id}") + public Account retrieve(@PathVariable Long id) { + return new Account(id); + } + + @RequestMapping(method = RequestMethod.DELETE, path = "/{id}") + public void remove(@PathVariable Long id) { + // ... + } +} \ No newline at end of file diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/WebConfig.java b/spring-rest-simple/src/main/java/com/baeldung/cors/WebConfig.java new file mode 100644 index 0000000000..dc579ce4ba --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/cors/WebConfig.java @@ -0,0 +1,16 @@ +package com.baeldung.cors; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**"); + } +} \ No newline at end of file diff --git a/spring-rest-simple/src/main/webapp/WEB-INF/spring-web-config.xml b/spring-rest-simple/src/main/webapp/WEB-INF/spring-web-config.xml new file mode 100644 index 0000000000..07d50ae1d6 --- /dev/null +++ b/spring-rest-simple/src/main/webapp/WEB-INF/spring-web-config.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + +