Introducing Code-first Java SDK for Kalix
- Renato Cavalcanti.
- Principal Engineer, Lightbend.
- 13 Apr 2023,
- 2 minute read
The Java SDK for Kalix offers a developer experience that is familiar to both Java and Spring Boot users.
It allows developers to assemble a Kalix application by annotating Java classes. Compared to other SDKs for Kalix, it frees the developer from describing their API with Protocol Buffers.
When using the Kalix Java SDK, your services are exposed using Spring REST annotations and serialization is backed by the ubiquitous Jackson library. And, most importantly, you can start coding directly in Java.
Watch the accompanying short webinar (15min) 👇
Or continue reading the blog 👇
To give you a quick preview of how it looks to define an Entity
, the code below shows a Kalix ValueEntity
for an imaginary Customer
model:
package customer;
import kalix.javasdk.valueentity.ValueEntity;
import kalix.javasdk.annotations.EntityKey;
import kalix.javasdk.annotations.EntityType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@EntityType("customer")
public class CustomerEntity extends ValueEntity<CustomerEntity.Customer> {
record Customer(String name, String email){}
record CreateCustomer(String name, String email){}
@EntityKey("customer_id")
@PostMapping("/customers/{customer_id}")
public Effect<String> createCustomer(@RequestBody CreateCustomer create) {
return effects()
.updateState(new Customer(create.name, create.email))
.thenReply("done");
}
}
When deployed to Kalix, clients can call the service using any http client. The createCustomer
method is exposed as a regular REST endpoint using Spring’s REST annotations.
To query Customers by name, you can write:
package customer;
import kalix.javasdk.view.View;
import kalix.javasdk.annotations.Query;
import kalix.javasdk.annotations.Subscribe;
import kalix.javasdk.annotations.Table;
import org.springframework.web.bind.annotation.GetMapping;
import reactor.core.publisher.Flux;
@Table("customers")
@Subscribe.ValueEntity(CustomerEntity.class)
public class CustomerByNameView extends View<CustomerEntity.Customer> {
@GetMapping("/customers/by-name/{name}")
@Query("SELECT * FROM customers WHERE name = :name")
public Flux<CustomerEntity.Customer> findCustomers(String name) {
return null;
}
}
The Kalix Java SDK detects all Kalix components under the main package and registers them automatically to Kalix.
That’s basically all that is needed to implement an Entity with query capabilities in Kalix. Just start coding!
You can try it out yourself by starting a new project using the Maven archetype or by downloading the quickstart sample.
To start a new Maven project
mvn archetype:generate
-DarchetypeGroupId=io.kalix
-DarchetypeArtifactId=kalix-spring-boot-archetype
-DarchetypeVersion=1.2.0
To download a fully-functional quickstart project
kalix quickstart download customer-registry-java
You can find more details in the new Kalix Java SDK documentation.
As a new feature we welcome feedback from initial users. Please contact us in the Kalix Forum with any suggestions or report issues on GitHub.