Introducing Code-first Java SDK for Kalix

By Kalix
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.

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 {

        record Customer(String name, String email){}
        record CreateCustomer(String name, String email){}
        
        @EntityKey("customer_id")
        @PostMapping("/customers/{customer_id}")
        public Effect 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 {
        
        @GetMapping("/customers/by-name/{name}")
        @Query("SELECT * FROM customers WHERE name = :name")
        public Flux 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.1.1
    

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.

Share
Share

Featured Articles

How digital twins unlock enterprises’ sustainability efforts

With sustainability increasingly on corporate and government agendas, over half of enterprises believing digital twin technology is critical to ESG efforts

Avast: Cybercriminals use common apps to lure victims

Two out of three cyber threats now leverage social engineering, with attackers using common applications from Microsoft and Adobe to distribute malware

World Password Day: Study shows enthusiasm for passwordless

Over half of global respondents told a study that they are excited about passwordless authentication options like biometrics, passkeys, or security keys

SAP to accelerate AI innovation with IBM Watson

AI & Machine Learning

Half of organisations fell victim to ransomware attacks

Cloud & Cybersecurity

Nike and Cognizant expand their relationship into technology

Digital Transformation