Kontent.ai
Copyright © 2023 Kontent.ai. All rights reserved.
  • Web
  • Privacy policy
  • Cookies policy
  • Consent settings
  • Security
  • GDPR
  • Documentation
  • API reference
  • Product updates
Kontent.ai Learn
  • Plan
  • Set up
  • Model
  • Develop
  • Create

Turn your content types into strongly typed models

Is this page helpful?
Complete and continue
  • Sign in
    • TypeScript
      TypeScript
    • .NET
      .NET
    • Java
      Java
    Jan Cerman
    10 minutes
    Delivery API
    Improve your development experience by using strongly typed models based on your Kontent.ai content types.

    Life without strongly typed models

    When getting content directly from the Delivery REST API, you receive content items as JSON objects. Your app then needs to parse the JSON to display your content. For example, to access the Headline element in a content item, you'd use a notation like this response.data.item.elements.headline.value.
    In a more complicated scenario, this approach becomes cumbersome. It forces you to remember the JSON structure of your content items and the codenames of your content elements. To make this easier, use a Delivery SDK to map the retrieved content items to their strongly typed models.

    Use strongly typed models

    This practice has several advantages:
    • type safety during compile-time
    • convenience (IntelliSense remembers content type properties for you)
    • support of type-dependent functionalities
    The models are plain classes with no attached behavior or dependency on an external framework. Each model corresponds to a content type in your Kontent.ai project.
    To create a content type’s representation in code, you need a class with properties representing the individual content elements:
    • Each property is mapped to its content element's codename either explicitly or using a naming convention. For example, codename body_text becomes property bodyText.
    • Properties are usually typed using classes provided by the SDK.
    • Depending on the SDK, simple content elements (like text and number) are sometimes represented using native types (like string and integer).
    The following example is a strongly typed model of the Homepage content type.

    Generate models

    Use the model generator to create strongly typed models from your Kontent.ai project by providing your project’s environment ID.

    Get strongly typed content

    With your models defined and added to your application, you can use them when retrieving items from Kontent.ai.
    What's next?
    Resolve rich text content
    Apart from structured and formatted text, rich text elements can also contain references to content components and content items, and links to content items.Let’s see how to deal with these references so your rich text is displayed correctly.
    • Develop with Kontent.ai
    • Hello World
    • Hello Web Spotlight
    • Run a sample app
    • Build apps
    • Decide navigation and URLs
    • Environments
    • Get Developer Certification
    We recommend that you generate the models using the Java model generator.
    • Generate models
    • Resolve rich text
    • Resolve linked content
    • Build your first app
    • Java
    • Java
    • Java
    // Tip: Find more about Java SDK at https://kontent.ai/learn/java
    import kontent.ai.delivery.Asset;
    import kontent.ai.delivery.ContentItemMapping;
    import kontent.ai.delivery.ElementMapping;
    import kontent.ai.delivery.System;
    import java.lang.String;
    import java.util.List;
    
    /**
     * This code was generated by a <a href="https://github.com/kontent-ai/java-packages/tree/master/delivery-sdk-generators">kontent-generators-java tool</a>
     *
     * Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
     * For further modifications of the class, create a separate file and extend this class.
     */
    @ContentItemMapping("homepage")
    public class Homepage {
        @ElementMapping("body_text")
        String bodyText;
    
        @ElementMapping("headline")
        String headline;
    
        @ElementMapping("picture")
        List<Asset> picture;
    
        System system;
    
        public String getBodyText() {
            return bodyText;
        }
    
        public void setBodyText(String bodyText) {
            this.bodyText = bodyText;
        }
    
        public String getHeadline() {
            return headline;
        }
    
        public void setHeadline(String headline) {
            this.headline = headline;
        }
    
        public List<Asset> getPicture() {
            return picture;
        }
    
        public void setPicture(List<Asset> picture) {
            this.picture = picture;
        }
    
        public System getSystem() {
            return system;
        }
    
        public void setSystem(System system) {
            this.system = system;
        }
    }
    // Find instructions on using the Java model generator at https://github.com/kontent-ai/java-packages/tree/master/delivery-sdk-generators
    import com.squareup.javapoet.JavaFile
    import kontent.ai.delivery.DeliveryClient
    import kontent.ai.delivery.DeliveryOptions
    import kontent.ai.delivery.generators.CodeGenerator
    
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath('ai.kontent:kontent-delivery-generators:latest.release')
        }
    }
    
    // showcase task
    task generateModels {
        doLast {
    
            // The most complex solution, you could configure the client as you want
            // i.e. set preview API key
            DeliveryOptions options = new DeliveryOptions();
            options.setProjectId("975bf280-fd91-488c-994c-2f04416e5ee3");
            DeliveryClient client = new DeliveryClient(options);
    
            CodeGenerator generator = new CodeGenerator(
                options.getProjectId(),
                'ai.kontent.test.springapp.models',
                file('src/main/java')
            );
            List<JavaFile> sources = generator.generateSources(client);
            generator.writeSources(sources);
        }
    }
    // Tip: Find more about Java SDK at https://kontent.ai/learn/java
    import kontent.ai.delivery.*;
    
    // Initializes a DeliveryClient
    DeliveryClient client = new DeliveryClient("8d20758c-d74c-4f59-ae04-ee928c0816b7");
    
    // Create strongly typed models according to https://kontent.ai/learn/strongly-typed-models
    // Registers the model class for navigation items
    client.registerType(Homepage.class);
    
    CompletionStage<Homepage> homepageResult = client.getItem("hello_caas_world", Homepage.class);
    // Use homepageResult
    // homepageResult.thenAccept(homepage -> System.out.println(homepage.getHeadline())
  • Life without strongly typed models
  • Use strongly typed models
  • Generate models
  • Get strongly typed content