Skip to content

Commit b881b04

Browse files
Introduce Spring Boot starter (#393)
1 parent 0f20059 commit b881b04

File tree

12 files changed

+512
-0
lines changed

12 files changed

+512
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
plugins {
2+
`java-conventions`
3+
`java-library`
4+
`test-jar-conventions`
5+
`library-publishing-conventions`
6+
id("io.spring.dependency-management") version "1.1.6"
7+
}
8+
9+
description = "Restate SDK Spring Boot starter"
10+
11+
val springBootVersion = "3.3.5"
12+
13+
java { toolchain { languageVersion = JavaLanguageVersion.of(17) } }
14+
15+
dependencies {
16+
compileOnly(coreLibs.jspecify)
17+
18+
api(project(":sdk-common")) {
19+
// Let spring bring jackson in
20+
exclude(group = "com.fasterxml.jackson")
21+
exclude(group = "com.fasterxml.jackson.core")
22+
exclude(group = "com.fasterxml.jackson.datatype")
23+
}
24+
api(project(":sdk-api")) {
25+
// Let spring bring jackson in
26+
exclude(group = "com.fasterxml.jackson")
27+
exclude(group = "com.fasterxml.jackson.core")
28+
exclude(group = "com.fasterxml.jackson.datatype")
29+
}
30+
api(project(":sdk-serde-jackson")) {
31+
// Let spring bring jackson in
32+
exclude(group = "com.fasterxml.jackson")
33+
exclude(group = "com.fasterxml.jackson.core")
34+
exclude(group = "com.fasterxml.jackson.datatype")
35+
}
36+
37+
implementation(project(":sdk-http-vertx")) {
38+
// Let spring bring jackson in
39+
exclude(group = "com.fasterxml.jackson")
40+
exclude(group = "com.fasterxml.jackson.core")
41+
exclude(group = "com.fasterxml.jackson.datatype")
42+
}
43+
implementation(project(":sdk-request-identity"))
44+
implementation(platform(vertxLibs.vertx.bom)) {
45+
// Let spring bring jackson in
46+
exclude(group = "com.fasterxml.jackson")
47+
exclude(group = "com.fasterxml.jackson.core")
48+
exclude(group = "com.fasterxml.jackson.datatype")
49+
}
50+
implementation(vertxLibs.vertx.core) {
51+
// Let spring bring jackson in
52+
exclude(group = "com.fasterxml.jackson")
53+
exclude(group = "com.fasterxml.jackson.core")
54+
exclude(group = "com.fasterxml.jackson.datatype")
55+
}
56+
57+
implementation("org.springframework.boot:spring-boot-starter:$springBootVersion")
58+
59+
// Spring is going to bring jackson in with this
60+
implementation("org.springframework.boot:spring-boot-starter-json:$springBootVersion")
61+
62+
// We need these for the deployment manifest
63+
testImplementation(project(":sdk-core"))
64+
testImplementation(platform(jacksonLibs.jackson.bom))
65+
testImplementation(jacksonLibs.jackson.annotations)
66+
testImplementation(jacksonLibs.jackson.databind)
67+
68+
testAnnotationProcessor(project(":sdk-api-gen"))
69+
testImplementation(project(":sdk-serde-jackson"))
70+
71+
testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
72+
}
73+
74+
tasks.withType<JavaCompile> { options.compilerArgs.add("-parameters") }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import java.lang.annotation.*;
12+
import org.springframework.context.annotation.Import;
13+
14+
/**
15+
* Add this annotation in your application class next to the {@link
16+
* org.springframework.boot.autoconfigure.SpringBootApplication} annotation to enable the Restate
17+
* Spring features.
18+
*
19+
* @see RestateComponent
20+
* @see RestateClientAutoConfiguration
21+
*/
22+
@Target(ElementType.TYPE)
23+
@Retention(RetentionPolicy.RUNTIME)
24+
@Documented
25+
@Import({RestateHttpEndpointBean.class, RestateClientAutoConfiguration.class})
26+
public @interface EnableRestate {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import dev.restate.sdk.client.Client;
12+
import java.util.Collections;
13+
import java.util.Map;
14+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
17+
18+
/**
19+
* Configuration for Restate's {@link Client}, to send requests to Restate services.
20+
*
21+
* @see RestateClientProperties
22+
*/
23+
@Configuration
24+
@EnableConfigurationProperties(RestateClientProperties.class)
25+
public class RestateClientAutoConfiguration {
26+
27+
@Bean
28+
public Client client(RestateClientProperties restateClientProperties) {
29+
Map<String, String> headers = restateClientProperties.getHeaders();
30+
if (headers == null) {
31+
headers = Collections.emptyMap();
32+
}
33+
return Client.connect(restateClientProperties.getBaseUri(), headers);
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import java.util.Map;
12+
import org.springframework.boot.context.properties.ConfigurationProperties;
13+
import org.springframework.boot.context.properties.bind.ConstructorBinding;
14+
import org.springframework.boot.context.properties.bind.DefaultValue;
15+
16+
@ConfigurationProperties(prefix = "restate.client")
17+
public class RestateClientProperties {
18+
19+
private final String baseUri;
20+
private final Map<String, String> headers;
21+
22+
@ConstructorBinding
23+
public RestateClientProperties(
24+
@DefaultValue(value = "http://localhost:8080") String baseUri, Map<String, String> headers) {
25+
this.baseUri = baseUri;
26+
this.headers = headers;
27+
}
28+
29+
/** Base uri of the Restate client, e.g. {@code http://localhost:8080}. */
30+
public String getBaseUri() {
31+
return baseUri;
32+
}
33+
34+
/** Headers added to each request sent to Restate. */
35+
public Map<String, String> getHeaders() {
36+
return headers;
37+
}
38+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import java.lang.annotation.*;
12+
import org.springframework.stereotype.Component;
13+
14+
/**
15+
* Add this annotation to a class annotated with Restate's {@link
16+
* dev.restate.sdk.annotation.Service} or {@link dev.restate.sdk.annotation.VirtualObject} or {@link
17+
* dev.restate.sdk.annotation.Workflow} to bind them to the Restate HTTP Endpoint.
18+
*
19+
* <p>You can configure the Restate HTTP Endpoint using {@link RestateEndpointProperties} and {@link
20+
* RestateEndpointHttpServerProperties}.
21+
*/
22+
@Target(ElementType.TYPE)
23+
@Retention(RetentionPolicy.RUNTIME)
24+
@Documented
25+
@Component
26+
public @interface RestateComponent {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import org.springframework.boot.context.properties.ConfigurationProperties;
12+
import org.springframework.boot.context.properties.bind.ConstructorBinding;
13+
import org.springframework.boot.context.properties.bind.DefaultValue;
14+
import org.springframework.boot.context.properties.bind.Name;
15+
16+
@ConfigurationProperties(prefix = "restate.sdk.http")
17+
public class RestateEndpointHttpServerProperties {
18+
19+
private final int port;
20+
21+
@ConstructorBinding
22+
public RestateEndpointHttpServerProperties(@Name("port") @DefaultValue(value = "9080") int port) {
23+
this.port = port;
24+
}
25+
26+
/** Port to expose the HTTP server. */
27+
public int getPort() {
28+
return port;
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.springboot;
10+
11+
import dev.restate.sdk.auth.RequestIdentityVerifier;
12+
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
13+
import org.springframework.boot.context.properties.ConfigurationProperties;
14+
import org.springframework.boot.context.properties.bind.ConstructorBinding;
15+
import org.springframework.boot.context.properties.bind.DefaultValue;
16+
17+
@ConfigurationProperties(prefix = "restate.sdk")
18+
public class RestateEndpointProperties {
19+
20+
private final boolean enablePreviewContext;
21+
private final String identityKey;
22+
23+
@ConstructorBinding
24+
public RestateEndpointProperties(
25+
@DefaultValue(value = "false") boolean enablePreviewContext, String identityKey) {
26+
this.enablePreviewContext = enablePreviewContext;
27+
this.identityKey = identityKey;
28+
}
29+
30+
/**
31+
* @see RestateHttpEndpointBuilder#enablePreviewContext()
32+
*/
33+
public boolean isEnablePreviewContext() {
34+
return enablePreviewContext;
35+
}
36+
37+
/**
38+
* @see RestateHttpEndpointBuilder#withRequestIdentityVerifier(RequestIdentityVerifier)
39+
*/
40+
public String getIdentityKey() {
41+
return identityKey;
42+
}
43+
}

0 commit comments

Comments
 (0)