Skip to content

Commit f17f65d

Browse files
authored
Merge pull request #1080 from mipo256/KTLN-587
KTLN-587
2 parents e0dcb9f + a6a9c08 commit f17f65d

File tree

40 files changed

+683
-154
lines changed

40 files changed

+683
-154
lines changed

kotlin-dsl/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>kotlin-dsl</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<name>kotlin-dsl</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-boot-3</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>../parent-boot-3</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<sourceDirectory>src/main/kotlin</sourceDirectory>
31+
<testSourceDirectory>src/test/java</testSourceDirectory>
32+
<plugins>
33+
<plugin>
34+
<artifactId>kotlin-maven-plugin</artifactId>
35+
<groupId>org.jetbrains.kotlin</groupId>
36+
<version>${kotlin.version}</version>
37+
<executions>
38+
<execution>
39+
<id>compile</id>
40+
<phase>compile</phase>
41+
<goals>
42+
<goal>compile</goal>
43+
</goals>
44+
</execution>
45+
<execution>
46+
<id>test-compile</id>
47+
<phase>test-compile</phase>
48+
<goals>
49+
<goal>test-compile</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
<configuration>
54+
<args>
55+
<arg>-Xjsr305=strict</arg>
56+
</args>
57+
<jvmTarget>1.8</jvmTarget>
58+
<compilerPlugins>
59+
<plugin>spring</plugin>
60+
<plugin>jpa</plugin>
61+
</compilerPlugins>
62+
</configuration>
63+
<dependencies>
64+
<dependency>
65+
<groupId>org.jetbrains.kotlin</groupId>
66+
<artifactId>kotlin-maven-allopen</artifactId>
67+
<version>${kotlin.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.jetbrains.kotlin</groupId>
71+
<artifactId>kotlin-maven-noarg</artifactId>
72+
<version>${kotlin.version}</version>
73+
</dependency>
74+
</dependencies>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.functionaldsl.beandeclration
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import org.springframework.context.support.BeanDefinitionDsl
6+
import org.springframework.context.support.beans
7+
import java.math.BigDecimal
8+
9+
@SpringBootApplication
10+
open class Application
11+
12+
fun main(vararg args: String) {
13+
runApplication<Application>(*args) {
14+
addInitializers(
15+
beans {
16+
bean(
17+
name = "functionallyDeclaredBean",
18+
scope = BeanDefinitionDsl.Scope.SINGLETON,
19+
isLazyInit = false,
20+
isPrimary = false,
21+
function = {
22+
BigDecimal(1.0)
23+
},
24+
)
25+
},
26+
)
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.functionaldsl.regularcontroller
2+
3+
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.PathVariable
5+
import org.springframework.web.bind.annotation.RequestHeader
6+
import org.springframework.web.bind.annotation.RequestParam
7+
import org.springframework.web.bind.annotation.RestController
8+
9+
@RestController
10+
class RegularController {
11+
@GetMapping(path = ["/endpoint/{country}"])
12+
fun getPerson(
13+
@RequestParam name: String,
14+
@RequestHeader(name = "X-age") age: String,
15+
@PathVariable country: String,
16+
): Map<*, *> {
17+
return mapOf(
18+
"name" to name,
19+
"age" to age,
20+
"country" to country,
21+
)
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.functionaldsl.routerfunction
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import org.springframework.context.annotation.Bean
6+
import org.springframework.web.servlet.function.RouterFunction
7+
import org.springframework.web.servlet.function.RouterFunctions
8+
import org.springframework.web.servlet.function.ServerResponse
9+
10+
@SpringBootApplication
11+
open class Application {
12+
@Bean
13+
open fun configure(): RouterFunction<ServerResponse> {
14+
return RouterFunctions.route()
15+
.GET("/endpoint/{country}") {
16+
ServerResponse.ok().body(
17+
mapOf(
18+
"name" to it.param("name"),
19+
"age" to it.headers().header("X-age")[0],
20+
"country" to it.pathVariable("country"),
21+
),
22+
)
23+
}
24+
.build()
25+
}
26+
}
27+
28+
fun main(vararg args: String) {
29+
runApplication<Application>(*args)
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.functionaldsl.routerfunctiondsl
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import org.springframework.context.support.beans
6+
import org.springframework.web.servlet.function.ServerRequest
7+
import org.springframework.web.servlet.function.ServerResponse
8+
import org.springframework.web.servlet.function.router
9+
10+
@SpringBootApplication
11+
open class Application
12+
13+
fun main(vararg args: String) {
14+
runApplication<Application>(*args) {
15+
addInitializers(
16+
beans {
17+
bean {
18+
router {
19+
GET("/endpoint/{country}") { it: ServerRequest ->
20+
ServerResponse.ok().body(
21+
mapOf(
22+
"name" to it.param("name"),
23+
"age" to it.headers().header("X-age")[0],
24+
"country" to it.pathVariable("country"),
25+
),
26+
)
27+
}
28+
}
29+
}
30+
},
31+
)
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.functionaldsl.regularcontroller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
5+
import com.baeldung.functionaldsl.regularcontroller.RegularController;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
@WebMvcTest(controllers = RegularController.class)
18+
@ContextConfiguration(classes = RegularController.class)
19+
public class RegularControllerUnitTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Test
27+
void whenGetOnRouterEndpoint_thenCorrectResponseReturned() throws Exception {
28+
Map<String, Object> result = objectMapper.readValue(mockMvc
29+
.perform(get("/endpoint/anything").header("X-age", 22).param("name", "Mikhail"))
30+
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
31+
.andReturn()
32+
.getResponse()
33+
.getContentAsString(), new TypeReference<Map<String, Object>>() {
34+
});
35+
36+
Assertions.assertThat(result).containsKeys("name", "age", "country");
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.functionaldsl.routerfunction;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
5+
import com.baeldung.functionaldsl.routerfunction.Application;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
@WebMvcTest(controllers = Application.class)
18+
@ContextConfiguration(classes = Application.class)
19+
public class RouterFunctionUnitTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Test
27+
void whenGetOnRouterEndpoint_thenCorrectResponseReturned() throws Exception {
28+
Map<String, Object> result = objectMapper.readValue(mockMvc
29+
.perform(get("/endpoint/anything").header("X-age", 22).param("name", "Mikhail"))
30+
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
31+
.andReturn()
32+
.getResponse()
33+
.getContentAsString(), new TypeReference<Map<String, Object>>() {
34+
});
35+
36+
Assertions.assertThat(result).containsKeys("name", "age", "country");
37+
}
38+
}

kotlin-reactive-dsl/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>kotlin-reactive-dsl</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<name>kotlin-reactive-dsl</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-boot-3</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>../parent-boot-3</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-webflux</artifactId>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<sourceDirectory>src/main/kotlin</sourceDirectory>
27+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
28+
<plugins>
29+
<plugin>
30+
<artifactId>kotlin-maven-plugin</artifactId>
31+
<executions>
32+
<execution>
33+
<id>compile</id>
34+
<phase>compile</phase>
35+
<goals>
36+
<goal>compile</goal>
37+
</goals>
38+
</execution>
39+
<execution>
40+
<id>test-compile</id>
41+
<phase>test-compile</phase>
42+
<goals>
43+
<goal>test-compile</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
<groupId>org.jetbrains.kotlin</groupId>
48+
<version>${kotlin.version}</version>
49+
<configuration>
50+
<args>
51+
<arg>-Xjsr305=strict</arg>
52+
</args>
53+
<jvmTarget>1.8</jvmTarget>
54+
<compilerPlugins>
55+
<plugin>spring</plugin>
56+
<plugin>jpa</plugin>
57+
</compilerPlugins>
58+
</configuration>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.jetbrains.kotlin</groupId>
62+
<artifactId>kotlin-maven-allopen</artifactId>
63+
<version>${kotlin.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.jetbrains.kotlin</groupId>
67+
<artifactId>kotlin-maven-noarg</artifactId>
68+
<version>${kotlin.version}</version>
69+
</dependency>
70+
</dependencies>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.functional_dsl
2+
3+
class User(
4+
var userId: Long,
5+
var name: String,
6+
var age: Int,
7+
var country: String,
8+
)

0 commit comments

Comments
 (0)