Skip to content

Commit dd8bca0

Browse files
committed
[FIX] #26 SpringBootCors
1 parent 34b1d4a commit dd8bca0

File tree

9 files changed

+106
-22
lines changed

9 files changed

+106
-22
lines changed

SpringBootCors/build.gradle

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
1-
buildscript {
2-
ext {
3-
springBootVersion = '2.1.3.RELEASE'
4-
}
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10-
}
1+
plugins {
2+
id 'org.springframework.boot' version '2.1.3.RELEASE'
3+
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
4+
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
5+
id "org.jetbrains.kotlin.plugin.jpa" version "1.2.71"
116
}
127

13-
apply plugin: 'java'
14-
apply plugin: 'eclipse'
15-
apply plugin: 'org.springframework.boot'
168
apply plugin: 'io.spring.dependency-management'
179

1810
group = 'com.example'
1911
version = '0.0.1-SNAPSHOT'
20-
sourceCompatibility = 1.8
12+
sourceCompatibility = '1.8'
2113

2214
repositories {
2315
mavenCentral()
2416
}
2517

2618
dependencies {
27-
compile("org.springframework.boot:spring-boot-starter-web")
28-
testCompile("org.springframework.boot:spring-boot-starter-test")
29-
}
19+
implementation 'org.springframework.boot:spring-boot-starter-web'
20+
implementation 'org.springframework.boot:spring-boot-starter-webflux'
21+
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
22+
implementation 'org.jetbrains.kotlin:kotlin-reflect'
23+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
24+
25+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
26+
}
27+
28+
compileKotlin {
29+
kotlinOptions {
30+
freeCompilerArgs = ['-Xjsr305=strict']
31+
jvmTarget = '1.8'
32+
}
33+
}
34+
35+
compileTestKotlin {
36+
kotlinOptions {
37+
freeCompilerArgs = ['-Xjsr305=strict']
38+
jvmTarget = '1.8'
39+
}
40+
}

SpringBootCors/src/main/java/com/tistory/heowc/SpringBootCorsApplication.java renamed to SpringBootCors/src/main/java/com/example/java/SpringBootCorsApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.tistory.heowc;
1+
package com.example.java;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;

SpringBootCors/src/main/java/com/tistory/heowc/config/WebConfig.java renamed to SpringBootCors/src/main/java/com/example/java/config/WebConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.tistory.heowc.config;
1+
package com.example.java.config;
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;

SpringBootCors/src/main/java/com/tistory/heowc/domain/Message.java renamed to SpringBootCors/src/main/java/com/example/java/domain/Message.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.tistory.heowc.domain;
1+
package com.example.java.domain;
22

33
public class Message {
44

SpringBootCors/src/main/java/com/tistory/heowc/web/MessageController.java renamed to SpringBootCors/src/main/java/com/example/java/web/MessageController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.tistory.heowc.web;
1+
package com.example.java.web;
22

3-
import com.tistory.heowc.domain.Message;
3+
import com.example.java.domain.Message;
44
import org.springframework.web.bind.annotation.*;
55

66
@RestController
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.kotlin
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
class SpringBootCorsApplication
8+
9+
fun main(args: Array<String>) {
10+
runApplication<SpringBootCorsApplication>(*args)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.kotlin.config
2+
3+
import org.springframework.context.annotation.Bean
4+
import org.springframework.context.annotation.Configuration
5+
import org.springframework.http.HttpMethod
6+
import org.springframework.web.servlet.config.annotation.CorsRegistry
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
8+
9+
@Configuration
10+
class WebConfig {
11+
12+
@Bean
13+
fun webMvcConfigurer(): WebMvcConfigurer {
14+
return object : WebMvcConfigurer {
15+
override fun addCorsMappings(registry: CorsRegistry) {
16+
registry.addMapping("/message/**")
17+
.allowedOrigins("*")
18+
.allowedMethods(HttpMethod.POST.name)
19+
.allowCredentials(false)
20+
.maxAge(3600)
21+
}
22+
}
23+
}
24+
25+
// => webflux 사용시 작성법
26+
//
27+
// @Bean
28+
// fun webFluxConfigurer(): WebFluxConfigurer {
29+
// return object : WebFluxConfigurer {
30+
// override fun addCorsMappings(registry: CorsRegistry) {
31+
// registry.addMapping("/message/**")
32+
// .allowedOrigins("*")
33+
// .allowedMethods(HttpMethod.POST.name)
34+
// .allowCredentials(false)
35+
// .maxAge(3600)
36+
// }
37+
// }
38+
// }
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.example.kotlin.domain
2+
3+
data class Message(val sender: String, val content: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.kotlin.web
2+
3+
import com.example.kotlin.domain.Message
4+
import org.springframework.web.bind.annotation.*
5+
6+
@RestController
7+
@RequestMapping("message")
8+
class MessageController {
9+
10+
//@CrossOrigin
11+
@GetMapping("{value}")
12+
operator fun get(@PathVariable value: String): String {
13+
return value
14+
}
15+
16+
@PostMapping
17+
fun post(@RequestBody message: Message): Message {
18+
return message
19+
}
20+
}

0 commit comments

Comments
 (0)