Skip to content

Using the Spring Boot Configuration and DI to Instantiate the DeliveryClient

Ondřej Chrastina edited this page Oct 4, 2022 · 2 revisions

Spring Boot and DI to register delivery client

You can use Spring @Configuration and @Bean data annotation to register the Kontent Delivery client and then used @Autowired when you need to use it

The approach is showcased on Spring Boot sample application - the code part is in ~/sample-app-spring-boot/src/main/java/kentico/kontent/delivery/sample/dancinggoat/springboot/KontentConfiguration.java file.

You can see there you can register recovers as well. The template is simple.

// ...

@Configuration
public class KontentConfiguration {

    @Bean
    public DeliveryClient deliveryClient() {
        DeliveryClient client = new DeliveryClient(
                DeliveryOptions
                        .builder()
                        .projectId("975bf280-fd91-488c-994c-2f04416e5ee3")
                        .customHeaders(Arrays.asList(
                                new Header(TRACKING_HEADER_NAME, TRACKING_HEADER_VALUE)
                        ))
                        .build()
        );

    // ... additional configuration of the client

    }
}

And then in a (most probably) controller, you just use the client:

// ...

@Controller
public class ArticleController {

    @Autowired
    DeliveryClient deliveryClient;

// ...

}