Browse Source

Enable kotlin route DSL (#2825)

* Add kotlin-maven-plugin to server module

* Add reactor-kotlin-extensions dependency

* Enable RouteDslTests.kt

* Enable kotlin compile and test case of kotlin route
pull/2842/head
Junho, Kim 2 years ago committed by GitHub
parent
commit
e007e6fa30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 88
      spring-cloud-gateway-sample/pom.xml
  2. 33
      spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/AdditionalRoutesImportSelector.java
  3. 2
      spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java
  4. 3
      spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java
  5. 83
      spring-cloud-gateway-server/pom.xml
  6. 2
      spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/AdhocTestSuite.java
  7. 5
      spring-cloud-gateway-server/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt

88
spring-cloud-gateway-sample/pom.xml

@ -92,6 +92,44 @@ @@ -92,6 +92,44 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<jvmTarget>17</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -135,55 +173,5 @@ @@ -135,55 +173,5 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>javaLowerThan16</id>
<activation>
<jdk>(,16)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

33
spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/AdditionalRoutesImportSelector.java

@ -1,33 +0,0 @@ @@ -1,33 +0,0 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.sample;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
class AdditionalRoutesImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
if (ClassUtils.isPresent("org.springframework.cloud.gateway.sample.AdditionalRoutes", null)) {
return new String[] { "org.springframework.cloud.gateway.sample.AdditionalRoutes" };
}
return new String[0];
}
}

2
spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java

@ -41,7 +41,7 @@ import org.springframework.web.reactive.function.server.ServerResponse; @@ -41,7 +41,7 @@ import org.springframework.web.reactive.function.server.ServerResponse;
*/
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(AdditionalRoutesImportSelector.class)
@Import(AdditionalRoutes.class)
public class GatewaySampleApplication {
public static final String HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS = "hello from fake /actuator/metrics/spring.cloud.gateway.requests";

3
spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java

@ -26,8 +26,6 @@ import org.junit.jupiter.api.AfterAll; @@ -26,8 +26,6 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -159,7 +157,6 @@ public class GatewaySampleApplicationTests { @@ -159,7 +157,6 @@ public class GatewaySampleApplicationTests {
}
@Test
@DisabledForJreRange(min = JRE.JAVA_16)
public void routeFromKotlin() {
webClient.get().uri("/anything/kotlinroute").header("Host", "kotlin.abc.org").exchange().expectHeader()
.valueEquals("X-TestHeader", "foobar").expectStatus().isOk();

83
spring-cloud-gateway-server/pom.xml

@ -88,6 +88,11 @@ @@ -88,6 +88,11 @@
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-extra</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor.kotlin</groupId>
<artifactId>reactor-kotlin-extensions</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-protobuf</artifactId>
@ -221,6 +226,40 @@ @@ -221,6 +226,40 @@
</dependencies>
<build>
<plugins>
<plugin>
<!-- Based on instructions here - https://kotlinlang.org/docs/reference/using-maven.html -->
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<configuration>
<jvmTarget>17</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -271,50 +310,6 @@ @@ -271,50 +310,6 @@
</plugins>
</build>
<profiles>
<profile>
<id>javaLowerThan16</id>
<activation>
<jdk>(,16)</jdk>
</activation>
<build>
<plugins>
<plugin>
<!-- Based on instructions here - https://kotlinlang.org/docs/reference/using-maven.html -->
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>java13plus</id>
<activation>

2
spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/AdhocTestSuite.java

@ -101,7 +101,7 @@ import static org.junit.Assume.assumeThat; @@ -101,7 +101,7 @@ import static org.junit.Assume.assumeThat;
org.springframework.cloud.gateway.route.RouteTests.class,
org.springframework.cloud.gateway.route.CachingRouteLocatorTests.class,
org.springframework.cloud.gateway.route.RouteRefreshListenerTests.class,
// org.springframework.cloud.gateway.route.builder.RouteDslTests.class,
org.springframework.cloud.gateway.route.builder.RouteDslTests.class,
org.springframework.cloud.gateway.route.builder.RouteBuilderTests.class,
org.springframework.cloud.gateway.route.builder.GatewayFilterSpecTests.class,
org.springframework.cloud.gateway.route.CachingRouteDefinitionLocatorTests.class,

5
spring-cloud-gateway-server/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt

@ -24,13 +24,12 @@ import org.springframework.cloud.gateway.support.ServerWebExchangeUtils @@ -24,13 +24,12 @@ import org.springframework.cloud.gateway.support.ServerWebExchangeUtils
import org.springframework.context.annotation.Configuration
import org.springframework.mock.http.server.reactive.MockServerHttpRequest
import org.springframework.mock.web.server.MockServerWebExchange
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.toMono
import reactor.kotlin.core.publisher.toMono
import reactor.test.StepVerifier
import java.net.URI
@SpringBootTest(classes = arrayOf(Config::class))
@SpringBootTest(classes = [Config::class])
class RouteDslTests {
@Autowired

Loading…
Cancel
Save