blog

Introducing Spring SDK for Kalix

Renato Cavalcanti.
Principal Engineer, Lightbend.
  • 30 Nov 2022,
  • 2 minute read

The new Spring SDK for Kalix offers a developer experience that is familiar to 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 Spring 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.

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.springsdk.annotations.EntityKey;
import kalix.springsdk.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.springsdk.annotations.Query;
import kalix.springsdk.annotations.Subscribe;
import kalix.springsdk.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 Spring SDK detects all Kalix components under the main package and registers them to Kalix just as a regular Spring Boot application does.

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.1.1

To download a fully-functional quickstart project

kalix quickstart download customer-registry-spring

You can find more details in the new Kalix Spring 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.

Author Section will go here