Introducing Code-first Java SDK for Kalix

By Kalix
Sponsored Content
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

Gen AI Boom Drives Nvidia Value to Overtake Microsoft

Nvidia surpasses Microsoft to become the most valuable company, with its AI and chip developments tripling stock and prompting a US$3.3tn market cap

IBM & Wimbledon: AI Is Changing the Game for Sports

IBM and The All England Lawn Tennis Club have unveiled AI features for Wimbledon that will provide real-time analysis and expanded, personalised content

Zoom: Powering EMEA with a Partner-Led Focus

We examine how Zoom is moving towards greater digital transformation via its EMEA partnership channels, inspiring the next generation of collaboration

Schneider Electric: UK&I President Grows Her Europe Presence

Digital Transformation

DTW24 Ignite: AI to Power the Next Generation of Technology

Digital Transformation

SolarWinds: IT Professionals Worry about AI Integration Risk

AI & Machine Learning