<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spring Boot Archives - Codersee blog- Kotlin on the backend</title>
	<atom:link href="https://blog.codersee.com/tag/spring-boot/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Kotlin &#38; Backend Tutorials - Learn Through Practice.</description>
	<lastBuildDate>Thu, 17 Apr 2025 09:17:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.codersee.com/wp-content/uploads/2025/04/cropped-codersee_logo_circle_2-32x32.png</url>
	<title>Spring Boot Archives - Codersee blog- Kotlin on the backend</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Spring for GraphQL with Kotlin Coroutines</title>
		<link>https://blog.codersee.com/spring-graphql-kotlin-coroutines/</link>
					<comments>https://blog.codersee.com/spring-graphql-kotlin-coroutines/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Sat, 12 Apr 2025 12:10:34 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[GraphQL]]></category>
		<category><![CDATA[Kotlin coroutines]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=19012915</guid>

					<description><![CDATA[<p>In this tutorial, we will learn how to expose GraphQL APIs using Spring for GraphQL and Kotlin coroutines.</p>
<p>The post <a href="https://blog.codersee.com/spring-graphql-kotlin-coroutines/">Spring for GraphQL with Kotlin Coroutines</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>At the end of this article, you will know precisely how to create a <strong>Spring Boot GraphQL</strong> application that exposes <strong>queries</strong> and <strong>mutations</strong> with the help of Kotlin <strong>suspended functions</strong> and <strong>Flows</strong>. </p>



<p>Long story short, we will see: </p>



<ul class="wp-block-list">
<li>how to prepare a GraphQL schema,</li>



<li>what imports are necessary to utilize suspended functions,</li>



<li>how to expose queries, mutations, and map schema with annotated controllers,</li>



<li>exception handling with <code>GraphQlExceptionHandler</code> </li>
</ul>



<h2 class="wp-block-heading" id="h-create-project">Create Project </h2>



<p>As the first step, let&#8217;s prepare a brand new project. </p>



<p>To do so, let&#8217;s navigate to the <a href="https://start.spring.io/" target="_blank" rel="noreferrer noopener">Spring Initializr</a> page and select the following imports: </p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="537" src="http://blog.codersee.com/wp-content/uploads/2025/04/spring_for_graphql_webflux_spring_initializr_page_screenshot-1024x537.png" alt="Image is a screenshot from Spring Initializr page and shows necessary spring configs for the tutorial, among others, Spring Reactive Web and Spring for GraphQL" class="wp-image-19012919" srcset="https://blog.codersee.com/wp-content/uploads/2025/04/spring_for_graphql_webflux_spring_initializr_page_screenshot-1024x537.png 1024w, https://blog.codersee.com/wp-content/uploads/2025/04/spring_for_graphql_webflux_spring_initializr_page_screenshot-300x157.png 300w, https://blog.codersee.com/wp-content/uploads/2025/04/spring_for_graphql_webflux_spring_initializr_page_screenshot-768x403.png 768w, https://blog.codersee.com/wp-content/uploads/2025/04/spring_for_graphql_webflux_spring_initializr_page_screenshot.png 1530w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>The project <strong>metadata</strong> are totally up to you, but when it comes to the <strong>dependencies</strong>, we must select the <em>Spring Reactive Web</em> and <em>Spring for GraphQL</em>.</p>



<p>Thanks to the first one, Spring Boot configures a <strong>reactive web stack</strong> based on <strong>Project Reactor</strong> and <strong>Netty</strong> instead of Tomcat. And given we want to work with GraphQL <strong>and </strong>Kotlin coroutines, then this is a must-have for us. </p>



<p>The second provides support for Spring applications built on GraphQL Java. Moreover, it is the <strong>successor</strong> of the GraphQL Java Spring project from the GraphQL Java team.</p>



<p>Lastly, let&#8217;s download the zip package, extract and import it to our IDE. </p>



<h2 class="wp-block-heading" id="h-define-graphql-schema">Define GraphQL Schema</h2>



<h3 class="wp-block-heading" id="h-what-is-graphql-schema">What is GraphQL Schema?</h3>



<p>If you have ever worked with GraphQL before, then you know that compared to the REST, everything starts with <strong>schema definition</strong>.</p>



<p>If you haven&#8217;t, then <strong>GraphQL schema</strong> is a file typically written with <strong>Schema Definition Language (SDL)</strong> that describes how our API clients can interact with our server. To be more specific, inside it, we define the structure of returned data, as well as how consumers can fetch or send us the payloads. </p>



<p>And even though it may sound like a GraphQL substitute for OpenAPI specs, it is something more. In REST, OpenAPI docs are a nice addition to our project. In GraphQL, our services continuously utilize the schema definition to validate and execute requests against it. </p>



<p>And if you would like to learn more about GraphQL schema, then check out the <a href="https://graphql.org/learn/schema/">official documentation</a> later. But for now, let&#8217;s focus on the Spring / GraphQL / Kotlin combination 😉 </p>



<h3 class="wp-block-heading" id="h-schema-graphql-in-spring">schema.graphql in Spring </h3>



<p>As the first step, let&#8217;s head to the <code>resources &gt; graphql</code> directory in our project, and let&#8217;s insert the <code>schema.graphql</code> file:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="type Query {
  article(id: ID!): Article
  articles: [Article!]!
}

type Mutation {
  createArticle(input: CreateArticleInput!): Article!
  addComment(articleId: ID!, input: AddCommentInput!): Comment
}

input CreateArticleInput {
  title: String!
  content: String!
  userId: ID!
}

input AddCommentInput {
  userId: String!
  content: String!
}

type User {
  id: ID!
  name: String!
}

type Article {
  id: ID!
  title: String!
  content: String!
  author: User!
  comments: [Comment!]!
  createdAt: String!
}

type Comment {
  id: ID!
  content: String!
  author: User
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">type</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Query</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  article(id: ID!): Article</span></span>
<span class="line"><span style="color: #E1E4E8">  articles: [Article!]!</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">type</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Mutation</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  createArticle(input: CreateArticleInput!): Article!</span></span>
<span class="line"><span style="color: #E1E4E8">  addComment(articleId: ID!, input: AddCommentInput!): Comment</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">input CreateArticleInput {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #B392F0">title</span><span style="color: #E1E4E8">: String</span><span style="color: #F97583">!</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #B392F0">content</span><span style="color: #E1E4E8">: String</span><span style="color: #F97583">!</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #B392F0">userId</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">ID</span><span style="color: #F97583">!</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">input AddCommentInput {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #B392F0">userId</span><span style="color: #E1E4E8">: String</span><span style="color: #F97583">!</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #B392F0">content</span><span style="color: #E1E4E8">: String</span><span style="color: #F97583">!</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">type</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  id: ID!</span></span>
<span class="line"><span style="color: #E1E4E8">  name: String!</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">type</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  id: ID!</span></span>
<span class="line"><span style="color: #E1E4E8">  title: String!</span></span>
<span class="line"><span style="color: #E1E4E8">  content: String!</span></span>
<span class="line"><span style="color: #E1E4E8">  author: User!</span></span>
<span class="line"><span style="color: #E1E4E8">  comments: [Comment!]!</span></span>
<span class="line"><span style="color: #E1E4E8">  createdAt: String!</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">type</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  id: ID!</span></span>
<span class="line"><span style="color: #E1E4E8">  content: String!</span></span>
<span class="line"><span style="color: #E1E4E8">  author: User</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>In Spring Boot, this file is registered automatically as our schema definition. </p>



<p>And as we can see, we defined 5 things: </p>



<ul class="wp-block-list">
<li><strong>queries</strong>&#8211; how a client reads data. Every GraphQL schema <strong>must </strong>support them,</li>



<li><strong>mutations</strong>&#8211; how a client modifies data,     </li>



<li><strong>inputs</strong>&#8211; used to pass structured arguments to our mutations and queries,</li>



<li><strong>types</strong>&#8211; establishing the structure of data we return,</li>



<li><strong>scalars</strong>&#8211; like <code>String</code>, or <code>ID</code> (but also <code>Int</code>, <code>Float</code>, <code>Boolean</code>), describing returned fields.</li>
</ul>



<p>Moreover, we can see the exclamation mark- <code>!</code>. In contrast to Kotlin, in GraphQL, we must <strong>explicitly define non-nullable types.</strong> Meaning, that <code>[Comment]</code> describes a nullable array of nullable <code>Comment</code> type (<code>Array&lt;Comment?&gt;?</code>), whereas <code>[Comment!]!</code> is a not null array with not null items.</p>



<h3 class="wp-block-heading" id="h-spring-graphql-schema-inspection">Spring GraphQL Schema Inspection</h3>



<p>Following, let&#8217;s rerun our application. </p>



<p>As a result, we should get the following in logs: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="o.s.b.a.g.GraphQlAutoConfiguration: GraphQL schema inspection:
Unmapped fields: {Query=[article, articles], Mutation=[createArticle, addComment]}
Unmapped registrations: {}
Unmapped arguments: {}
Skipped types: []" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #d8dee9ff">o.s.b.a.g.GraphQlAutoConfiguration: GraphQL schema inspection:</span></span>
<span class="line"><span style="color: #d8dee9ff">Unmapped fields: {Query=[article, articles], Mutation=[createArticle, addComment]}</span></span>
<span class="line"><span style="color: #d8dee9ff">Unmapped registrations: {}</span></span>
<span class="line"><span style="color: #d8dee9ff">Unmapped arguments: {}</span></span>
<span class="line"><span style="color: #d8dee9ff">Skipped types: []</span></span></code></pre></div>



<p>As we can see, Spring Boot verifies the schema we defined every time we start the application. And the above message simply says that <strong>we do not have handler methods for our definition.</strong></p>



<p>Let&#8217;s fix that then 😉</p>



<h2 class="wp-block-heading" id="h-create-models">Create Models </h2>



<p>As the next step, let&#8217;s add Kotlin data classes to our Spring Boot project. They will be later used to serialize and deserialize data defined in our GraphQL schema.</p>



<p>To do so, let&#8217;s add the <code>Models.kt</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="data class User(
    val id: String,
    val name: String,
)
data class Article(
    val id: String,
    val title: String,
    val content: String,
    val authorId: String,
    val createdAt: String,
)
data class Comment(
    val id: String,
    val content: String,
    val articleId: String,
    val userId: String,
)
data class CreateArticleInput(
    val title: String,
    val content: String,
    val userId: String,
)
data class AddCommentInput(
    val content: String,
    val userId: String,
)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> name: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> title: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> content: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> authorId: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> createdAt: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> content: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> articleId: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> userId: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CreateArticleInput</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> title: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> content: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> userId: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AddCommentInput</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> content: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> userId: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span></code></pre></div>



<p>As we can see, nothing spectacular here. The only interesting fact is that the <code>ID</code> is serialized in the same way as a <code>String</code>.</p>



<h2 class="wp-block-heading" id="h-querymapping-as-suspend-fun">QueryMapping as suspend fun</h2>



<p>Following, let&#8217;s learn how the Spring for GraphQL works with Kotlin coroutines. </p>



<p>According to the documentation:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Kotlin coroutine and <code>Flow</code> are adapted to <code>Mono</code> and <code>Flux</code>.</p>
</blockquote>



<p>Which means that whenever we use the Spring WebFlux, <strong>Spring automatically converts our suspended functions <code>Mono</code> and functions that return <code>Flow</code> to <code>Flux</code></strong> .</p>



<h3 class="wp-block-heading" id="h-implement-article-service">Implement Article Service</h3>



<p>Before we head to the GraphQL part, let&#8217;s make some small preparations.</p>



<p>Let&#8217;s insert the <code>ArticleService</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@Component
object ArticleService {
    private val articles = mutableListOf(
        Article(
            id = &quot;article-id-1&quot;,
            title = &quot;article-title-1&quot;,
            content = &quot;article-content-1&quot;,
            authorId = &quot;user-id-2&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
        Article(
            id = &quot;article-id-2&quot;,
            title = &quot;article-title-2&quot;,
            content = &quot;article-content-2&quot;,
            authorId = &quot;user-id-2&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
        Article(
            id = &quot;article-id-3&quot;,
            title = &quot;article-title-3&quot;,
            content = &quot;article-content-3&quot;,
            authorId = &quot;user-id-3&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
    )
    suspend fun findArticleById(id: String): Article? {
        delay(100)
        return articles.firstOrNull { it.id == id }
    }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@Component</span></span>
<span class="line"><span style="color: #F97583">object</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArticleService</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> articles </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">mutableListOf</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">    )</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findArticleById</span><span style="color: #E1E4E8">(id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">? {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">delay</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">100</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> articles.</span><span style="color: #B392F0">firstOrNull</span><span style="color: #E1E4E8"> { it.id </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> id }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As we can see, we use my favorite, in-memory database called <strong>mutable list</strong> (😉) and expose <code>findArticleById</code> &#8211; a suspended function.</p>



<p>This function will either find the desired article by ID or return a null value.</p>



<h3 class="wp-block-heading" id="h-add-articlecontroller">Add ArticleController</h3>



<p>With that done, let&#8217;s add the <code>ArticleController</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@Controller
class ArticleController(
    private val articleService: ArticleService,
) {
    @QueryMapping
    suspend fun article(@Argument id: String): Article? =
        articleService.findArticleById(id)
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@Controller</span></span>
<span class="line"><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArticleController</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> articleService: </span><span style="color: #B392F0">ArticleService</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">) {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">@QueryMapping</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">article</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">@Argument</span><span style="color: #E1E4E8"> id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">? </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">        articleService.</span><span style="color: #B392F0">findArticleById</span><span style="color: #E1E4E8">(id)</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As we can see, thanks to the Spring GraphQL, we can leverage the annotation-based approach.</p>



<p>Firstly, we must annotate our class with the <strong>@Controller </strong>annotation. Then, all the methods inside it annotated with <strong>@SchemaMapping</strong> annotation will become handlers. </p>



<p>And <strong>@QueryMapping, @MutationMapping, </strong>or <strong>@SubscriptionMapping</strong> are nothing else than meta annotations (annotated with @SchemaMapping). This way, we achieve a more readable code. </p>



<p>Additionally, in Spring, we use the <code><strong>@Argument</strong></code> annotation to bind GraphQL input arguments to our Kotlin instances. </p>



<h3 class="wp-block-heading" id="h-enable-graphiql-amp-test">Enable GraphiQL &amp; Test</h3>



<p>Nextly, let&#8217;s turn on the <strong>GraphiQL</strong>&#8211; the IDE for testing GraphQL APIs (like Postman, Bruno, or Insomnia).</p>



<p>To do so, let&#8217;s navigate to the <code>resources</code> directory and change the <code>application.properties</code> into the <code>application.yaml</code> file:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="spring:
  application:
    name: graphsandbox
  graphql:
    graphiql:
      enabled: true" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #85E89D">spring</span><span style="color: #E1E4E8">:</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #85E89D">application</span><span style="color: #E1E4E8">:</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #85E89D">name</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">graphsandbox</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #85E89D">graphql</span><span style="color: #E1E4E8">:</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #85E89D">graphiql</span><span style="color: #E1E4E8">:</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #85E89D">enabled</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">true</span></span></code></pre></div>



<p>Then, let&#8217;s open up the browser, go to <code>http://localhost:8080/graphiql</code>, and put the following: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="query SomeRandomQuery {
  article(id: &quot;article-id-1&quot;) {
    id
    title
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">query</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">SomeRandomQuery</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #FFAB70">article</span><span style="color: #E1E4E8">(</span><span style="color: #FFAB70">id</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">) {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">title</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As we can see, the <strong>query</strong> name is up to us, but inside it, we must define which of the &#8220;exposed&#8221; queries we would like to use. As the input parameter, we pass the String value- <code>article-id-1</code>&#8211; and lastly, we define what fields we would like to get in response (that&#8217;s what the whole GraphQL is a about, right?😉).</p>



<p>So, as the next step, let&#8217;s run the query and check out the result: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;errors&quot;: [
    {
      &quot;message&quot;: &quot;INTERNAL_ERROR for f337f3f3-5&quot;,
      &quot;locations&quot;: [
        {
          &quot;line&quot;: 2,
          &quot;column&quot;: 3
        }
      ],
      &quot;path&quot;: [
        &quot;article&quot;
      ],
      &quot;extensions&quot;: {
        &quot;classification&quot;: &quot;INTERNAL_ERROR&quot;
      }
    }
  ],
  &quot;data&quot;: {
    &quot;article&quot;: null
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;errors&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">    {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;message&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;INTERNAL_ERROR for f337f3f3-5&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;locations&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">        {</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;line&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;column&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">3</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">      ],</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;path&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #9ECBFF">&quot;article&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      ],</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;extensions&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;classification&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;INTERNAL_ERROR&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  ],</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;article&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">null</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>Unfortunately, that is not what we expected. </p>



<p>Moreover, when we check out logs, we should see this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="s.g.e.ExceptionResolversExceptionHandler : Unresolved NoClassDefFoundError for executionId f337f3f3-5
java.lang.NoClassDefFoundError: org/springframework/data/util/KotlinReflectionUtils
	at org.springframework.graphql.data.method.InvocableHandlerMethodSupport.invokeSuspendingFunction(InvocableHandlerMethodSupport.java:141) ~[spring-graphql-1.3.4.jar:1.3.4]
	at org.springframework.graphql.data.method.InvocableHandlerMethodSupport.doInvoke(InvocableHandlerMethodSupport.java:108) ~[spring-graphql-1.3.4.jar:1.3.4]" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">s.g.e.ExceptionResolversExceptionHandler : Unresolved NoClassDefFoundError for executionId f337f3f3-5</span></span>
<span class="line"><span style="color: #e1e4e8">java.lang.NoClassDefFoundError: org/springframework/data/util/KotlinReflectionUtils</span></span>
<span class="line"><span style="color: #e1e4e8">	at org.springframework.graphql.data.method.InvocableHandlerMethodSupport.invokeSuspendingFunction(InvocableHandlerMethodSupport.java:141) ~[spring-graphql-1.3.4.jar:1.3.4]</span></span>
<span class="line"><span style="color: #e1e4e8">	at org.springframework.graphql.data.method.InvocableHandlerMethodSupport.doInvoke(InvocableHandlerMethodSupport.java:108) ~[spring-graphql-1.3.4.jar:1.3.4]</span></span></code></pre></div>



<h3 class="wp-block-heading" id="h-fix-spring-graphql-coroutines-issue">Fix Spring GraphQL Coroutines Issue</h3>



<p>As we could see, the issue is caused by the missing dependency, and we can easily fix that.</p>



<p>To do so, let&#8217;s open up the <code>build.gradle.kts</code> and add the following dependency:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="implementation(&quot;org.springframework.data:spring-data-commons&quot;)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">implementation(&quot;org.springframework.data:spring-data-commons&quot;)</span></span></code></pre></div>



<p>Then, let&#8217;s sync the gradle project and rerun the application.</p>



<p>After we run the test again, we should see the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;data&quot;: {
    &quot;article&quot;: {
      &quot;id&quot;: &quot;article-id-1&quot;,
      &quot;title&quot;: &quot;article-title-1&quot;
    }
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;article&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-title-1&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>Splendid! Everything works perfectly fine😉</p>



<h2 class="wp-block-heading" id="h-spring-graphql-with-kotlin-flow">Spring GraphQL with Kotlin Flow </h2>



<p>With that done, let&#8217;s learn how to work with Kotlin Flow and Spring GraphQL.</p>



<p>As the first step, let&#8217;s insert a new function to our <code>ArticleService</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="fun findAllArticles(): Flow&lt;Article&gt; = articles.asFlow()" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findAllArticles</span><span style="color: #E1E4E8">(): </span><span style="color: #B392F0">Flow</span><span style="color: #E1E4E8">&lt;</span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">&gt; </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> articles.</span><span style="color: #B392F0">asFlow</span><span style="color: #E1E4E8">()</span></span></code></pre></div>



<p>This way, we produce a <strong>cold flow</strong> from our list of articles.</p>



<p>Then, let&#8217;s add a new handler:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@QueryMapping
fun articles(): Flow&lt;Article&gt; =
    articleService.findAllArticles()" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@QueryMapping</span></span>
<span class="line"><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">articles</span><span style="color: #E1E4E8">(): </span><span style="color: #B392F0">Flow</span><span style="color: #E1E4E8">&lt;</span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">&gt; </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">findAllArticles</span><span style="color: #E1E4E8">()</span></span></code></pre></div>



<p>Nothing new this time. Just like previously, we add a new function marked with <strong>@QueryMapping</strong>.</p>



<p>When we run the following query in GraphiQL: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="query AnotherOne {
  articles {
    id
    title
    createdAt
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">query</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AnotherOne</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #FFAB70">articles</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">title</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">createdAt</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>We should see the exact result: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;data&quot;: {
    &quot;articles&quot;: [
      {
        &quot;id&quot;: &quot;article-id-1&quot;,
        &quot;title&quot;: &quot;article-title-1&quot;,
        &quot;createdAt&quot;: &quot;2025-04-12T13:06:29.142469200&quot;
      },
      {
        &quot;id&quot;: &quot;article-id-2&quot;,
        &quot;title&quot;: &quot;article-title-2&quot;,
        &quot;createdAt&quot;: &quot;2025-04-12T13:06:29.142469200&quot;
      },
      {
        &quot;id&quot;: &quot;article-id-3&quot;,
        &quot;title&quot;: &quot;article-title-3&quot;,
        &quot;createdAt&quot;: &quot;2025-04-12T13:06:29.142469200&quot;
      }
    ]
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;articles&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">      {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-title-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;createdAt&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;2025-04-12T13:06:29.142469200&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      },</span></span>
<span class="line"><span style="color: #E1E4E8">      {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-title-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;createdAt&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;2025-04-12T13:06:29.142469200&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      },</span></span>
<span class="line"><span style="color: #E1E4E8">      {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-title-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;createdAt&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;2025-04-12T13:06:29.142469200&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    ]</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<h2 class="wp-block-heading" id="h-schemamapping">@SchemaMapping</h2>



<p>One of the greatest thing in GraphQL is the possibility to return the related data in one, single query. And if you remember our schema definition, our API should allow us to do that.</p>



<p>To be more specific, each article can have the <strong>author</strong>, multiple <strong>comments</strong>, and each comment also has its <strong>author</strong>.</p>



<p>But when we test that functionality right now:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="query AnotherOne {
  articles {
    id
    title
    createdAt
    author {
      id
      name
    }
    
    comments {
      id
      content
      author {
        name
      }
    }
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">query</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AnotherOne</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #FFAB70">articles</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">title</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">createdAt</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">author</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">name</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">    </span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">comments</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">content</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">author</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #FFAB70">name</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>The only thing we will get in response will be a looooong list of errors:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;errors&quot;: [
    {
      &quot;message&quot;: &quot;The field at path '/articles[0]/author' was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value.  The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. The non-nullable type is 'User' within parent type 'Article'&quot;,
      &quot;path&quot;: [
        &quot;articles&quot;,
        0,
        &quot;author&quot;
      ],
      &quot;extensions&quot;: {
        &quot;classification&quot;: &quot;NullValueInNonNullableField&quot;
      }
    }
... other errors" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">{</span></span>
<span class="line"><span style="color: #e1e4e8">  &quot;errors&quot;: [</span></span>
<span class="line"><span style="color: #e1e4e8">    {</span></span>
<span class="line"><span style="color: #e1e4e8">      &quot;message&quot;: &quot;The field at path &#39;/articles[0]/author&#39; was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value.  The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. The non-nullable type is &#39;User&#39; within parent type &#39;Article&#39;&quot;,</span></span>
<span class="line"><span style="color: #e1e4e8">      &quot;path&quot;: [</span></span>
<span class="line"><span style="color: #e1e4e8">        &quot;articles&quot;,</span></span>
<span class="line"><span style="color: #e1e4e8">        0,</span></span>
<span class="line"><span style="color: #e1e4e8">        &quot;author&quot;</span></span>
<span class="line"><span style="color: #e1e4e8">      ],</span></span>
<span class="line"><span style="color: #e1e4e8">      &quot;extensions&quot;: {</span></span>
<span class="line"><span style="color: #e1e4e8">        &quot;classification&quot;: &quot;NullValueInNonNullableField&quot;</span></span>
<span class="line"><span style="color: #e1e4e8">      }</span></span>
<span class="line"><span style="color: #e1e4e8">    }</span></span>
<span class="line"><span style="color: #e1e4e8">... other errors</span></span></code></pre></div>



<p>Of course, with Spring we can easily fix it, but we will need small preparation first.</p>



<h3 class="wp-block-heading" id="h-update-service">Update Service</h3>



<p>Firstly, let&#8217;s get back to our service and update it: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@Component
object ArticleService {
    private val articles = mutableListOf(
        Article(
            id = &quot;article-id-1&quot;,
            title = &quot;article-title-1&quot;,
            content = &quot;article-content-1&quot;,
            authorId = &quot;user-id-2&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
        Article(
            id = &quot;article-id-2&quot;,
            title = &quot;article-title-2&quot;,
            content = &quot;article-content-2&quot;,
            authorId = &quot;user-id-2&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
        Article(
            id = &quot;article-id-3&quot;,
            title = &quot;article-title-3&quot;,
            content = &quot;article-content-3&quot;,
            authorId = &quot;user-id-3&quot;,
            createdAt = LocalDateTime.now().toString()
        ),
    )
    private val users = mutableListOf(
        User(id = &quot;user-id-1&quot;, name = &quot;user-name-1&quot;),
        User(id = &quot;user-id-2&quot;, name = &quot;user-name-2&quot;),
        User(id = &quot;user-id-3&quot;, name = &quot;user-name-3&quot;),
    )
    private val comments = mutableListOf(
        Comment(id = &quot;comment-id-1&quot;, content = &quot;comment-content-1&quot;, articleId = &quot;article-id-1&quot;, userId = &quot;user-id-3&quot;),
        Comment(id = &quot;comment-id-2&quot;, content = &quot;comment-content-2&quot;, articleId = &quot;article-id-2&quot;, userId = &quot;user-id-3&quot;),
        Comment(id = &quot;comment-id-3&quot;, content = &quot;comment-content-3&quot;, articleId = &quot;article-id-2&quot;, userId = &quot;user-id-2&quot;),
        Comment(id = &quot;comment-id-4&quot;, content = &quot;comment-content-4&quot;, articleId = &quot;article-id-3&quot;, userId = &quot;user-id-3&quot;),
    )
    
    suspend fun findArticleById(id: String): Article? {
        delay(100)
        return articles.firstOrNull { it.id == id }
    }
    fun findAllArticles(): Flow&lt;Article&gt; = articles.asFlow()
    suspend fun findUserById(id: String): User? {
        delay(100)
        return users.firstOrNull { it.id == id }
    }
    fun findCommentsByArticleId(id: String): Flow&lt;Comment&gt; =
        comments.filter { it.articleId == id }.asFlow()
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@Component</span></span>
<span class="line"><span style="color: #F97583">object</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArticleService</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> articles </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">mutableListOf</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">            id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-title-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-content-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">        ),</span></span>
<span class="line"><span style="color: #E1E4E8">    )</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> users </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">mutableListOf</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-1&quot;</span><span style="color: #E1E4E8">, name </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-name-1&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">, name </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-name-2&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">, name </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-name-3&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">    )</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> comments </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">mutableListOf</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-id-1&quot;</span><span style="color: #E1E4E8">, content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-content-1&quot;</span><span style="color: #E1E4E8">, articleId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">, userId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-id-2&quot;</span><span style="color: #E1E4E8">, content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-content-2&quot;</span><span style="color: #E1E4E8">, articleId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-2&quot;</span><span style="color: #E1E4E8">, userId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-id-3&quot;</span><span style="color: #E1E4E8">, content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-content-3&quot;</span><span style="color: #E1E4E8">, articleId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-2&quot;</span><span style="color: #E1E4E8">, userId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-id-4&quot;</span><span style="color: #E1E4E8">, content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;comment-content-4&quot;</span><span style="color: #E1E4E8">, articleId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;article-id-3&quot;</span><span style="color: #E1E4E8">, userId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">),</span></span>
<span class="line"><span style="color: #E1E4E8">    )</span></span>
<span class="line"><span style="color: #E1E4E8">    </span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findArticleById</span><span style="color: #E1E4E8">(id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">? {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">delay</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">100</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> articles.</span><span style="color: #B392F0">firstOrNull</span><span style="color: #E1E4E8"> { it.id </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> id }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findAllArticles</span><span style="color: #E1E4E8">(): </span><span style="color: #B392F0">Flow</span><span style="color: #E1E4E8">&lt;</span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">&gt; </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> articles.</span><span style="color: #B392F0">asFlow</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findUserById</span><span style="color: #E1E4E8">(id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8">? {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">delay</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">100</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> users.</span><span style="color: #B392F0">firstOrNull</span><span style="color: #E1E4E8"> { it.id </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> id }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findCommentsByArticleId</span><span style="color: #E1E4E8">(id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Flow</span><span style="color: #E1E4E8">&lt;</span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">&gt; </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">        comments.</span><span style="color: #B392F0">filter</span><span style="color: #E1E4E8"> { it.articleId </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> id }.</span><span style="color: #B392F0">asFlow</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As we can see, we added two more &#8220;tables&#8221; along with simple functions to obtain data from them. </p>



<h3 class="wp-block-heading" id="h-update-controller">Update @Controller</h3>



<p>Then, let&#8217;s update our controller class:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@SchemaMapping
suspend fun author(article: Article): User =
    articleService.findUserById(article.authorId)!!
@SchemaMapping
suspend fun author(comment: Comment): User =
    articleService.findUserById(comment.userId)!!
@SchemaMapping
fun comments(article: Article): Flow&lt;Comment&gt; =
    articleService.findCommentsByArticleId(article.id)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@SchemaMapping</span></span>
<span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">author</span><span style="color: #E1E4E8">(article: </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">findUserById</span><span style="color: #E1E4E8">(article.authorId)</span><span style="color: #F97583">!!</span></span>
<span class="line"><span style="color: #B392F0">@SchemaMapping</span></span>
<span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">author</span><span style="color: #E1E4E8">(comment: </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">User</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">findUserById</span><span style="color: #E1E4E8">(comment.userId)</span><span style="color: #F97583">!!</span></span>
<span class="line"><span style="color: #B392F0">@SchemaMapping</span></span>
<span class="line"><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">comments</span><span style="color: #E1E4E8">(article: </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Flow</span><span style="color: #E1E4E8">&lt;</span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">&gt; </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">findCommentsByArticleId</span><span style="color: #E1E4E8">(article.id)</span></span></code></pre></div>



<p>As we can see, this time we leverage the <strong>@SchemaMapping</strong> annotation for our handlers. Pretty similar to what we did already.</p>



<p>The interesting part here is the <strong>parameters</strong>. Every handler takes the <strong>parent type as an argument</strong>. Meaning, that for the <code>article -&gt; author</code> parent-child relationship, we define the function that takes the article instance as an argument. And that&#8217;s it!</p>



<p>Of course, please the double exclamation (<code>!!</code>) should not land in the real, production-ready code😉</p>



<p>Anyway, when we rerun our test now, we will see the following: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;data&quot;: {
    &quot;articles&quot;: [
      {
        &quot;id&quot;: &quot;article-id-1&quot;,
        &quot;title&quot;: &quot;article-title-1&quot;,
        &quot;createdAt&quot;: &quot;2025-04-12T13:21:17.584507600&quot;,
        &quot;author&quot;: {
          &quot;id&quot;: &quot;user-id-2&quot;,
          &quot;name&quot;: &quot;user-name-2&quot;
        },
        &quot;comments&quot;: [
          {
            &quot;id&quot;: &quot;comment-id-1&quot;,
            &quot;content&quot;: &quot;comment-content-1&quot;,
            &quot;author&quot;: {
              &quot;name&quot;: &quot;user-name-3&quot;
            }
          }
        ]
      }
... more thingies " style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;articles&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">      {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;article-title-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;createdAt&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;2025-04-12T13:21:17.584507600&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;author&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-id-2&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;name&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-name-2&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">        },</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;comments&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">          {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;comment-id-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #79B8FF">&quot;content&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;comment-content-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #79B8FF">&quot;author&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">              </span><span style="color: #79B8FF">&quot;name&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-name-3&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">          }</span></span>
<span class="line"><span style="color: #E1E4E8">        ]</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #FDAEB7; font-style: italic">...</span><span style="color: #E1E4E8"> </span><span style="color: #FDAEB7; font-style: italic">more</span><span style="color: #E1E4E8"> </span><span style="color: #FDAEB7; font-style: italic">thingies</span><span style="color: #E1E4E8"> </span></span></code></pre></div>



<p>And that&#8217;s what we expected. Awesome!</p>



<h2 class="wp-block-heading" id="h-graphql-mutations">GraphQL Mutations </h2>



<p>When we run the application right now, we see that we still miss two things that we defined- <strong>mutations</strong>.</p>



<p>So, before we add a new handler, let&#8217;s implement the following function in our service:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="suspend fun createArticle(input: CreateArticleInput): Article {
    delay(100)
    return Article(
        id = UUID.randomUUID().toString(),
        title = input.title,
        content = input.content,
        authorId = input.userId,
        createdAt = LocalDateTime.now().toString(),
    ).also { articles.add(it) }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">createArticle</span><span style="color: #E1E4E8">(input: </span><span style="color: #B392F0">CreateArticleInput</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">delay</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">100</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">        id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> UUID.</span><span style="color: #B392F0">randomUUID</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">(),</span></span>
<span class="line"><span style="color: #E1E4E8">        title </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> input.title,</span></span>
<span class="line"><span style="color: #E1E4E8">        content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> input.content,</span></span>
<span class="line"><span style="color: #E1E4E8">        authorId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> input.userId,</span></span>
<span class="line"><span style="color: #E1E4E8">        createdAt </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> LocalDateTime.</span><span style="color: #B392F0">now</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">(),</span></span>
<span class="line"><span style="color: #E1E4E8">    ).</span><span style="color: #B392F0">also</span><span style="color: #E1E4E8"> { articles.</span><span style="color: #B392F0">add</span><span style="color: #E1E4E8">(it) }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As can be seen, we take the input, create a new <code>Article</code> instance, and insert that to the list.</p>



<p>With Kotlin <code>also</code> ,we can return at the same time the created instance and achieve a slightly cleaner code. To be even fancier, we could use a reference here: <code>.also(articles::add)</code> 😉 </p>



<p>Then, let&#8217;s add the appropriate handler to our controller:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@MutationMapping
suspend fun createArticle(@Argument input: CreateArticleInput): Article =
    articleService.createArticle(input)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@MutationMapping</span></span>
<span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">createArticle</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">@Argument</span><span style="color: #E1E4E8"> input: </span><span style="color: #B392F0">CreateArticleInput</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Article</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">createArticle</span><span style="color: #E1E4E8">(input)</span></span></code></pre></div>



<p>As we can see, just like with @QueryMapping, this time, we mark the handler with <strong>@MutationMapping</strong> and our argument with <strong>@Argument</strong>. Nothing else is necessary to make our mutation work. </p>



<p>So, given that, let&#8217;s rerun the app and test our functionality:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="mutation someMutation {
  createArticle(
    input: {
      title: &quot;Awesome codersee Kotlin article&quot;, 
      content: &quot;Some content&quot;, 
      userId: &quot;user-id-3&quot;
    }
  ) {
    id
    title
    content
    author {
      id
      name
    }
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">mutation</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">someMutation</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #FFAB70">createArticle</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">input</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #9ECBFF">title</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;Awesome codersee Kotlin article&quot;</span><span style="color: #E1E4E8">, </span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #9ECBFF">content</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;Some content&quot;</span><span style="color: #E1E4E8">, </span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #9ECBFF">userId</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  ) {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">title</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">content</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">author</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #FFAB70">name</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As a result, we should get the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;data&quot;: {
    &quot;createArticle&quot;: {
      &quot;id&quot;: &quot;0e51a902-835b-4e55-9b4a-f2dccd242ea7&quot;,
      &quot;title&quot;: &quot;Awesome codersee Kotlin article&quot;,
      &quot;content&quot;: &quot;Some content&quot;,
      &quot;author&quot;: {
        &quot;id&quot;: &quot;user-id-3&quot;,
        &quot;name&quot;: &quot;user-name-3&quot;
      }
    }
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;createArticle&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;0e51a902-835b-4e55-9b4a-f2dccd242ea7&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;title&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;Awesome codersee Kotlin article&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;content&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;Some content&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;author&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;id&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-id-3&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;name&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-name-3&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>Wonderful! We can see that with GraphQL mutations, we can not only create/modify things on our server. With this one query, we can also retrieve the data and the dependent objects😮</p>



<h2 class="wp-block-heading" id="h-error-handling-in-spring-graphql">Error Handling in Spring GraphQL</h2>



<p>As the last step, let&#8217;s add one more mutation to learn more about <strong>error handling</strong>.</p>



<p>First, let&#8217;s add the new custom exception ot our codebase in <code>Models.kt</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="data class GenericNotFound(val msg: String) : RuntimeException(msg)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">data</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">GenericNotFound</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">val</span><span style="color: #E1E4E8"> msg: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">) : </span><span style="color: #B392F0">RuntimeException</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">msg</span><span style="color: #E1E4E8">)</span></span></code></pre></div>



<p>Then, let&#8217;s implement the following function in <code>ArticleService</code>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="suspend fun addComment(id: String, input: AddCommentInput): Comment {
        delay(100)
        users.firstOrNull { it.id == input.userId }
            ?: throw GenericNotFound(&quot;User not found&quot;)
        return articles.firstOrNull { it.id == id }
            ?.let {
                Comment(
                    id = UUID.randomUUID().toString(),
                    articleId = id,
                    content = input.content,
                    userId = input.userId,
                ).also { comments.add(it) }
            }
            ?: throw GenericNotFound(&quot;Article not found&quot;)
    }" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">addComment</span><span style="color: #E1E4E8">(id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">, input: </span><span style="color: #B392F0">AddCommentInput</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">delay</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">100</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">        users.</span><span style="color: #B392F0">firstOrNull</span><span style="color: #E1E4E8"> { it.id </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> input.userId }</span></span>
<span class="line"><span style="color: #E1E4E8">            ?: </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">GenericNotFound</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;User not found&quot;</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> articles.</span><span style="color: #B392F0">firstOrNull</span><span style="color: #E1E4E8"> { it.id </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> id }</span></span>
<span class="line"><span style="color: #E1E4E8">            ?.</span><span style="color: #B392F0">let</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">                    id </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> UUID.</span><span style="color: #B392F0">randomUUID</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toString</span><span style="color: #E1E4E8">(),</span></span>
<span class="line"><span style="color: #E1E4E8">                    articleId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> id,</span></span>
<span class="line"><span style="color: #E1E4E8">                    content </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> input.content,</span></span>
<span class="line"><span style="color: #E1E4E8">                    userId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> input.userId,</span></span>
<span class="line"><span style="color: #E1E4E8">                ).</span><span style="color: #B392F0">also</span><span style="color: #E1E4E8"> { comments.</span><span style="color: #B392F0">add</span><span style="color: #E1E4E8">(it) }</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">            ?: </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">GenericNotFound</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;Article not found&quot;</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p>This time, we can see a more production-ready code.</p>



<p>If we want to add a comment to the article, we must first check if both the desired author and article exist, right?</p>



<p>If that is not the case, then we throw our custom exception.</p>



<p>Following, let&#8217;s add a new mutation handler: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@MutationMapping
suspend fun addComment(
    @Argument id: String,
    @Argument input: AddCommentInput,
): Comment =
    articleService.addComment(id, input)" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@MutationMapping</span></span>
<span class="line"><span style="color: #F97583">suspend</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">addComment</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">@Argument</span><span style="color: #E1E4E8"> id: </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">@Argument</span><span style="color: #E1E4E8"> input: </span><span style="color: #B392F0">AddCommentInput</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">): </span><span style="color: #B392F0">Comment</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">    articleService.</span><span style="color: #B392F0">addComment</span><span style="color: #E1E4E8">(id, input)</span></span></code></pre></div>



<p>Now, when we rerun the app and execute the following test: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="mutation anotherMutation {
  addComment(
    articleId: &quot;non-existing&quot;
    input: {userId: &quot;user-id-1&quot;, content: &quot;My comment&quot;}
  ) {
    id
    content
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #F97583">mutation</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">anotherMutation</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #FFAB70">addComment</span><span style="color: #E1E4E8">(</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">articleId</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;non-existing&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">input</span><span style="color: #E1E4E8">: {</span><span style="color: #9ECBFF">userId</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;user-id-1&quot;</span><span style="color: #E1E4E8">, </span><span style="color: #9ECBFF">content</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;My comment&quot;</span><span style="color: #E1E4E8">}</span></span>
<span class="line"><span style="color: #E1E4E8">  ) {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">id</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #FFAB70">content</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>We will see the following error: </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;errors&quot;: [
    {
      &quot;message&quot;: &quot;INTERNAL_ERROR for f62a4c7e-1&quot;,
      &quot;locations&quot;: [
        {
          &quot;line&quot;: 52,
          &quot;column&quot;: 3
        }
      ],
      &quot;path&quot;: [
        &quot;addComment&quot;
      ],
      &quot;extensions&quot;: {
        &quot;classification&quot;: &quot;INTERNAL_ERROR&quot;
      }
    }
  ],
  &quot;data&quot;: {
    &quot;addComment&quot;: null
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;errors&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">    {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;message&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;INTERNAL_ERROR for f62a4c7e-1&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;locations&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">        {</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;line&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">52</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">          </span><span style="color: #79B8FF">&quot;column&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">3</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">      ],</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;path&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #9ECBFF">&quot;addComment&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      ],</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;extensions&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;classification&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;INTERNAL_ERROR&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  ],</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;addComment&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">null</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>And this is not what we wanted, right? </p>



<h3 class="wp-block-heading" id="h-graphqlexceptionhandler-and-controlleradvice">GraphQlExceptionHandler and ControllerAdvice</h3>



<p>One of the solutions we can have in such a situation is a combination of ControllerAdvice and GraphQlExceptionHandler.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>If you would like to learn more about ControllerAdvice and RestControllerAdvice, then check out my <a href="https://blog.codersee.com/controlleradvice-vs-restcontrolleradvice/">other article</a>.</p>
</blockquote>



<p>So, as the next step, let&#8217;s add the <code>GlobalExceptionHandler</code> class to the project:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="@ControllerAdvice
class GlobalExceptionHandler {
    @GraphQlExceptionHandler
    fun handleGenericNotFound(ex: GenericNotFound): GraphQLError =
        GraphQLError.newError()
            .errorType(ErrorType.DataFetchingException)
            .message(ex.msg)
            .build()
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #B392F0">@ControllerAdvice</span></span>
<span class="line"><span style="color: #F97583">class</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">GlobalExceptionHandler</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">@GraphQlExceptionHandler</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">fun</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">handleGenericNotFound</span><span style="color: #E1E4E8">(ex: </span><span style="color: #B392F0">GenericNotFound</span><span style="color: #E1E4E8">): </span><span style="color: #B392F0">GraphQLError</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span></span>
<span class="line"><span style="color: #E1E4E8">        GraphQLError.</span><span style="color: #B392F0">newError</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">            .</span><span style="color: #B392F0">errorType</span><span style="color: #E1E4E8">(ErrorType.DataFetchingException)</span></span>
<span class="line"><span style="color: #E1E4E8">            .</span><span style="color: #B392F0">message</span><span style="color: #E1E4E8">(ex.msg)</span></span>
<span class="line"><span style="color: #E1E4E8">            .</span><span style="color: #B392F0">build</span><span style="color: #E1E4E8">()</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p>As we can see, inside it, we implement a function that takes the <strong>GenericNotFound</strong> as an argument. This way, whenever our custom exception is thrown, Spring will return the <strong>GraphQLError</strong> instance with our config instead.</p>



<p>As a result, when we retest the application, we should get the following JSON:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="{
  &quot;errors&quot;: [
    {
      &quot;message&quot;: &quot;Article not found&quot;,
      &quot;locations&quot;: [],
      &quot;extensions&quot;: {
        &quot;classification&quot;: &quot;DataFetchingException&quot;
      }
    }
  ],
  &quot;data&quot;: {
    &quot;addComment&quot;: null
  }
}" style="color:#e1e4e8;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">{</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;errors&quot;</span><span style="color: #E1E4E8">: [</span></span>
<span class="line"><span style="color: #E1E4E8">    {</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;message&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;Article not found&quot;</span><span style="color: #E1E4E8">,</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;locations&quot;</span><span style="color: #E1E4E8">: [],</span></span>
<span class="line"><span style="color: #E1E4E8">      </span><span style="color: #79B8FF">&quot;extensions&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">&quot;classification&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #9ECBFF">&quot;DataFetchingException&quot;</span></span>
<span class="line"><span style="color: #E1E4E8">      }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">  ],</span></span>
<span class="line"><span style="color: #E1E4E8">  </span><span style="color: #79B8FF">&quot;data&quot;</span><span style="color: #E1E4E8">: {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #79B8FF">&quot;addComment&quot;</span><span style="color: #E1E4E8">: </span><span style="color: #79B8FF">null</span></span>
<span class="line"><span style="color: #E1E4E8">  }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<h2 class="wp-block-heading" id="h-spring-graphql-with-kotlin-coroutines-summary">Spring GraphQL with Kotlin Coroutines Summary</h2>



<p>And that&#8217;s all for this article on how to work with Kotlin coroutines and Flows in the Spring Boot GraphQL project.</p>



<p>As always, you can find the source code in my <a href="https://github.com/codersee-blog/spring-boot-3-graphql-kotlin-coroutines" target="_blank" rel="noreferrer noopener">GitHub repostory</a>.</p>
<p>The post <a href="https://blog.codersee.com/spring-graphql-kotlin-coroutines/">Spring for GraphQL with Kotlin Coroutines</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/spring-graphql-kotlin-coroutines/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Spring Boot with Kotlin, AWS S3, and S3Template</title>
		<link>https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/</link>
					<comments>https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Tue, 17 Sep 2024 05:00:00 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[S3 Object Storage]]></category>
		<category><![CDATA[S3Template]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=12009151</guid>

					<description><![CDATA[<p>The second article in a series dedicated to Spring Boot AWS S3 integration focused on S3Template and Kotlin.</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/">Spring Boot with Kotlin, AWS S3, and S3Template</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Welcome to the <strong>second article</strong> in a series dedicated to integrating a Spring Boot Kotlin app with <strong>AWS S3 </strong>Object Storage, in which we will learn how to make our lives easier with <strong>S3Template</strong>. </p>



<p>Of course, I highly encourage you to take a look at other articles in this series, too: </p>



<ul class="wp-block-list">
<li><a href="https://blog.codersee.com/spring-boot-aws-s3-s3client-kotlin/">#1 Spring Boot with AWS S3, S3Client, and Kotlin</a></li>



<li>#2 (This article)</li>



<li><a href="https://blog.codersee.com/test-spring-boot-aws-s3-with-localstack-and-testcontainers/">#3 Test Spring Boot AWS S3 with Localstack and Testcontainers</a></li>
</ul>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video that covers all three articles:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<a href="https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2FuTV9w1JehHM%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p></div>



<p>If you find this content useful,<strong> please leave a subscription</strong> 🙂</p>



<h2 class="wp-block-heading" id="h-what-is-s3template">What is S3Template? </h2>



<p>Before we get our hands dirty with the code, let&#8217;s take a second to understand better with S3Template is and how it can make our lives easier when integrating a Spring Boot app with S3. </p>



<p>Let me quote the S3Template documentation here: </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Higher level abstraction over S3Client providing methods for the most common use cases.</p>



<p>So, if you have already worked with, or you have seen my previous article with S3Client, then you saw that simple operations require some boilerplate. And that is exactly what S3Template solves. </p>



<p>And as a note from my end, I just wanted to note S3Template handles only some subset of S3Client operations, so in our projects those two will rather coexist, instead of being each others alternatives. </p>
</blockquote>



<h2 class="wp-block-heading" id="h-aws-s3template-operations">AWS S3Template Operations</h2>



<p>With all of that said, let&#8217;s get to work. </p>



<p>Let&#8217;s add the <code>controller</code> package and <code>BucketController</code> class to it. We will use it to expose a bunch of endpoints triggering various operations on S3 buckets and files. </p>



<p>When it comes to the operations- we will use the same ones as in the previous article, and as the last one, I will show you how to <strong>serialize and deserialize objects with AWS S3 and S3Template.</strong></p>



<h3 class="wp-block-heading" id="h-list-all-buckets">List All Buckets</h3>



<p>Although the S3Template does not expose any method that would help us with this task, I wanted to mention it as we discussed it in the previous tutorial. </p>



<p>Additionally, this is a great example of <code>S3Client</code> and <code>S3Template</code> co-existence: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@RestController
@RequestMapping("/buckets")
class BucketController(
  private val s3Template: S3Template,
  private val s3Client: S3Client,
) {

  @GetMapping
  fun listBuckets(): List&lt;String> {
    val response = s3Client.listBuckets()

    return response.buckets()
      .mapIndexed { index, bucket ->
        "Bucket #${index + 1}: ${bucket.name()}"
      }
  }
}</pre>



<p>As we can see, not too much S3Template could improve here, so I bet this is the reason why it was not introduced. </p>



<h3 class="wp-block-heading" id="h-new-s3-bucket">New S3 Bucket</h3>



<p>Nextly, let&#8217;s take a look at how we can create a brand new bucket: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@PostMapping
fun createBucket(@RequestBody request: BucketRequest) {
  s3Template.createBucket(request.bucketName)
}

data class BucketRequest(val bucketName: String)</pre>



<p>As we can see, no additional request classes- the only thing we need is the bucket name. </p>



<p>Of course, let&#8217;s rerun our application and verify if everything is working: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request POST 'http://localhost:8080/buckets' \
--header 'Content-Type: application/json' \
--data-raw '{
    "bucketName": "codersee-awesome-bucket"
}'</pre>



<p>As a result, we should get 200 OK without a response body. </p>



<h3 class="wp-block-heading" id="h-upload-file-to-s3-bucket">Upload File to S3 Bucket</h3>



<p>Nextly, let&#8217;s take a look at how to upload a new file to S3. </p>



<p>We have a few options, among which the <code>store</code> function is the easiest: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@PostMapping("/{bucketName}/objects")
fun createObject(@PathVariable bucketName: String, @RequestBody request: ObjectRequest) {
  s3Template.store(bucketName, request.objectName, request.content)
}

data class ObjectRequest(val objectName: String, val content: String)</pre>



<p>As we can see, this function takes three arguments: </p>



<ul class="wp-block-list">
<li>the bucket name, </li>



<li>filename,</li>



<li>and the content to upload (to be specific <code>Object object</code>) </li>
</ul>



<p>Alternatively, we could use the <code>upload</code> function, which allows us to send <code>InputStream</code> instance and metadata: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Override
public S3Resource upload(
  String bucketName, 
  String key, 
  InputStream inputStream,
  @Nullable ObjectMetadata objectMetadata
) </pre>



<p>Lastly, let&#8217;s verify that everything is fine with the following curl: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request POST 'http://localhost:8080/buckets/codersee-awesome-bucket/objects' \
--header 'Content-Type: application/json' \
--data-raw '{
    "objectName": "file-example.txt",
    "content": "My file content"
}'</pre>



<p>If everything worked, then a new file should be present in our bucket 🙂 </p>



<h3 class="wp-block-heading" id="h-list-files-from-the-bucket">List Files From The Bucket</h3>



<p>Nextly, let&#8217;s see how we can list the bucket content with <em>S3Template</em>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@GetMapping("/{bucketName}/objects")
fun listObjects(@PathVariable bucketName: String): List&lt;String> =
  s3Template.listObjects(bucketName, "")
    .map { s3Resource -> s3Resource.filename }</pre>



<p>We can clearly see that this is not rocket science 😉 </p>



<p>Nevertheless, it is worth mentioning that this time we get the <code>S3Resource</code> instance and instead of the <code>key()</code> we use it <code>getFilename</code> method. </p>



<p>And just like previously, let&#8217;s see the endpoint in action: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request POST 'http://localhost:8080/buckets/codersee-awesome-bucket/objects' \
--header 'Content-Type: application/json' \
--data-raw '{
    "objectName": "file-example.txt",
    "content": "My file content"
}'

# Response status: 200 OK
# Response body: 
[
    "file-example.txt"
]</pre>



<h3 class="wp-block-heading" id="h-download-a-file">Download a File</h3>



<p>So what about fetching files from the S3 bucket? </p>



<p>With <em>S3Template</em>, it&#8217;s a piece of cake: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@GetMapping("/{bucketName}/objects/{objectName}")
fun getObject(@PathVariable bucketName: String, @PathVariable objectName: String): String =
  s3Template.download(bucketName, objectName).getContentAsString(UTF_8)</pre>



<p>We specify the bucket name and the object name, and as a result, we get the <code>S3Resource</code> that exposes a bunch of methods. Among others, the <code>getContentAsString</code> that is pretty descriptive 😉 </p>



<p>Similarly, let&#8217;s hit the endpoint: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request GET 'http://localhost:8080/buckets/codersee-awesome-bucket/objects/file-example.txt'

# Response status: 200 OK
# Response body: 
"My file content"</pre>



<h3 class="wp-block-heading" id="h-delete-the-bucket">Delete the Bucket</h3>



<p>Last before least, let&#8217;s take a look at how we can get rid of the bucket: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@DeleteMapping("/{bucketName}")
fun deleteBucket(@PathVariable bucketName: String) {
  s3Template.listObjects(bucketName, "")
    .forEach { s3Template.deleteObject(bucketName, it.filename) }

  s3Template.deleteBucket(bucketName)
}</pre>



<p>And just like in the previous article- we must ensure the bucket does not contain any objects. </p>



<p>To do so, we list out objects by specifying the bucket name and objects prefix (as we don&#8217;t have any, we pass an empty String). Then, for each object, we use its key to delete it. And lastly, we simply invoke the <code>deleteBucket</code> by passing the name of a bucket to delete. </p>



<p>Of course, let&#8217;s verify this logic, too: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request DELETE 'http://localhost:8080/buckets/codersee-awesome-bucket'</pre>



<p>If we run this command and the S3 bucket exists, then we should see 200 OK and our bucket will disappear. </p>



<h3 class="wp-block-heading" id="h-serialize-deserialize-objects">Serialize/Deserialize objects </h3>



<p>As the last thing, let&#8217;s take a look at how easily we can persist objects using the combination of <code>store</code> and <code>read</code> functions: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@PostMapping("/{bucketName}/objects")
fun createExampleObject(@PathVariable bucketName: String): Example {
  val example = Example(id = UUID.randomUUID(), name = "Some name")

  s3Template.store(bucketName, "example.json", example)

  return s3Template.read(bucketName, "example.json", Example::class.java)
}

data class Example(val id: UUID, val name: String)</pre>



<p>As we can see, we made a small update to our <code>POST /{bucketName}/objects</code> endpoint logic. </p>



<p>Basically, the first part is exactly the same, we use the <code>store</code> again to push the file to the bucket. </p>



<p>Nevertheless, instead of the <code>download</code> we saw previously, we use the <code>read</code> function that uses the <code>S3ObjectConverter</code> that will automatically deserialize the JSON into the <code>Example</code> class instance. </p>



<p>And for the last time, let&#8217;s hit our API: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request POST 'http://localhost:8080/buckets/codersee-awesome-bucket/objects' \
--data-raw ''

# Response status: 200 OK 
# Response body: 
{
    "id": "3eacd8a3-48b2-4756-86db-e4c9f4e291da",
    "name": "Some name"
}</pre>



<p>And as we can see, the output confirms that everything is working fine. </p>



<p>Summary</p>



<p>And that&#8217;s all for this article on how to make our lives easier in Spring Boot with S3Template. </p>



<p>I hope you enjoyed it, and again wanted to invite you to take a look at other content of this series: </p>



<ul class="wp-block-list">
<li><a href="https://blog.codersee.com/spring-boot-aws-s3-s3client-kotlin/">#1 Spring Boot with AWS S3, S3Client, and Kotlin</a></li>



<li>#2 (This article)</li>



<li><a href="https://blog.codersee.com/test-spring-boot-aws-s3-with-localstack-and-testcontainers/">#3 Test Spring Boot AWS S3 with Localstack and Testcontainers</a></li>
</ul>



<p>Lastly, just wanted to show that you can find the source code in <a href="https://github.com/codersee-blog/spring-boot-3-kotlin-s3template" target="_blank" rel="noreferrer noopener">this GitHub repository</a> and that you can join my <a href="https://codersee.com/newsletter/">newsletter</a> to stay up-to-date with Kotlin on the backend. </p>
<p>The post <a href="https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/">Spring Boot with Kotlin, AWS S3, and S3Template</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/spring-boot-with-kotlin-aws-s3-and-s3template/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sending Spring Boot Metrics to Datadog</title>
		<link>https://blog.codersee.com/sending-spring-boot-metrics-datadog/</link>
					<comments>https://blog.codersee.com/sending-spring-boot-metrics-datadog/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Tue, 27 Aug 2024 04:00:00 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Datadog]]></category>
		<category><![CDATA[Metrics]]></category>
		<category><![CDATA[Micrometer]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=11009048</guid>

					<description><![CDATA[<p>A step-by-step guide on how to implement and configure a Spring Boot application to send metrics to Datadog.</p>
<p>The post <a href="https://blog.codersee.com/sending-spring-boot-metrics-datadog/">Sending Spring Boot Metrics to Datadog</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hello and welcome to the next article in which we will briefly discuss Datadog and Micrometer, but what&#8217;s more important- we will learn how to send <strong>Spring Boot metrics</strong> to <strong>Datadog in practice</strong>.</p>



<p>Moreover, we will take a quick look into the Spring Boot Actuator and how it can help us, so, to let&#8217;s not waste our time and let&#8217;s get to work! 🙂 </p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<a href="https://blog.codersee.com/sending-spring-boot-metrics-datadog/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2F2YHVgbJGzqU%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p></div>



<p>If you find this content useful,<strong> please leave a subscription</strong> 🙂</p>



<h2 class="wp-block-heading" id="h-datadog">Datadog</h2>



<p>If you came here directly, then I bet you already know what <a href="https://www.datadoghq.com/" target="_blank" rel="noreferrer noopener">Datadog</a> is and what purpose it will serve in your project.</p>



<p>However, for those who hear about it for the first time, let me give you a really short intro.</p>



<p>Long story short- Datadog is an <strong>integrated platform for monitoring &amp; security</strong>. It provides tools for monitoring applications, databases, distributed tracing, logs management, and many many more. In other words, makes all the things that are not so cool, but necessary in production-ready environments, a bit less painful 😉</p>



<p>However, what is the most important for us today- it provides tools to <strong>collect metrics</strong> from our applications so that later we can make use of them.</p>



<p>But, what are they? </p>



<h2 class="wp-block-heading" id="h-what-are-metrics">What Are Metrics? </h2>



<p>According to the Datadog documentation:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Metrics are numerical values that can track anything about your environment over time.</p>
</blockquote>



<p>And I must admit- I love this definition for its completeness in its simplicity.</p>



<p>Because basically, <strong>metric</strong> is everything in our system that we can assign a number to and track. From Garbage Collector stats, JVM memory, CPU, and request latency, to the average cart size. Literally everything.</p>



<p>And in this tutorial, we will see how to send metrics <strong>collected automatically</strong> by our Spring Boot app to DataDog.</p>



<h2 class="wp-block-heading" id="h-create-new-project">Create New Project</h2>



<p>As the first step, let&#8217;s navigate to the <a href="https://start.spring.io/" target="_blank" rel="noreferrer noopener">Spring Initializr page</a>, select the following and import the project to our IDE:</p>



<figure class="wp-block-image aligncenter size-large is-resized"><img decoding="async" width="1024" height="511" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_actuator_1-1024x511.png" alt="Screenshot presents the Spring Initializr page with Spring Boot Actuator and Spring Web dependencies selected." class="wp-image-11009063" style="width:840px;height:auto" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_actuator_1-1024x511.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_actuator_1-300x150.png 300w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_actuator_1-768x383.png 768w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_actuator_1.png 1527w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Apart from versions and metadata, we can see that we added two dependencies: <strong>Spring Web</strong> and <strong>Spring Boot Actuator.</strong> </p>



<p>The first one is quite obvious, but in terms of metrics Spring Boot Actuator provides dependency management and auto-configuration for <strong>Micrometer</strong> &#8211; a vendor-neutral application observability facade. </p>



<p>With that, Spring Boot takes care of configuring <code>MeterRegistry</code> and the only thing we need to do is to add the <code>micrometer-registry-{system}</code> dependency to the <code>build.gradle.kts</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">implementation("io.micrometer:micrometer-registry-datadog")</pre>



<p><strong>Note:</strong> we could achieve exactly the same using the Spring Initializr page and searching for the Datadog dependency, but I wanted to emphasize the fact, that other vendors, like Elastic, or OpenTelemetry can be added in the same manner.  </p>



<h2 class="wp-block-heading" id="h-verify-and-expose-metrics-endpoint">Verify and Expose Metrics Endpoint</h2>



<p>After we import our project, let&#8217;s run it, and let&#8217;s take a look at the endpoints exposed by default by Spring Boot Actuator: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request GET 'http://localhost:8080/actuator' </pre>



<p>As a result, we should see the following: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        }
    }
}</pre>



<p>And the first lesson is that <strong>the metrics endpoint is not exposed by default.</strong></p>



<p>So to change that, let&#8217;s navigate to the <code>application.yaml</code> and add the following:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health</pre>



<p>After that, let&#8217;s rerun the application. </p>



<p>As a result, this time, we should see that two new endpoints are present: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",
            "templated": false
        }
    }
}</pre>



<p>The last one- <code>/actuator/metrics</code>&#8211; simply <strong>displays a list of available meter names</strong>, and when we invoke it, we should see the following JSON response: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
    "names": [
        "application.ready.time",
        "application.started.time",
        "disk.free",
        "disk.total",
        "executor.active",
        "executor.completed",
        "executor.pool.core",
        "executor.pool.max",
        "executor.pool.size",
        "executor.queue.remaining",
        "executor.queued",
        "http.server.requests",
        "http.server.requests.active",
        "jvm.buffer.count",
        "jvm.buffer.memory.used",
        "jvm.buffer.total.capacity",
        "jvm.classes.loaded",
        "jvm.classes.unloaded",
        "jvm.compilation.time",
        "jvm.gc.concurrent.phase.time",
        "jvm.gc.live.data.size",
        "jvm.gc.max.data.size",
        "jvm.gc.memory.allocated",
        "jvm.gc.memory.promoted",
        "jvm.gc.overhead",
        "jvm.gc.pause",
        "jvm.info",
        "jvm.memory.committed",
        "jvm.memory.max",
        "jvm.memory.usage.after.gc",
        "jvm.memory.used",
        "jvm.threads.daemon",
        "jvm.threads.live",
        "jvm.threads.peak",
        "jvm.threads.started",
        "jvm.threads.states",
        "logback.events",
        "process.cpu.time",
        "process.cpu.usage",
        "process.start.time",
        "process.uptime",
        "system.cpu.count",
        "system.cpu.usage",
        "tomcat.sessions.active.current",
        "tomcat.sessions.active.max",
        "tomcat.sessions.alive.max",
        "tomcat.sessions.created",
        "tomcat.sessions.expired",
        "tomcat.sessions.rejected"
    ]
}</pre>



<p>In simple words, this is the list of all metrics collected at this point by our Spring Boot application. And yes- we will be able to export them to Datadog. </p>



<p>Nevertheless, let&#8217;s verify what the second endpoint- <code>/actuator/metrics/{requiredMetricName}</code>&#8211; does.</p>



<p>To do that, let&#8217;s make a GET request and specify any metric name:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl --location --request GET 'http://localhost:8080/actuator/metrics/process.cpu.usage'</pre>



<p>This time, the endpoint will return metric details along with the <strong>current value</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
    "name": "process.cpu.usage",
    "description": "The \"recent cpu usage\" for the Java Virtual Machine process",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 0.038209510879134205
        }
    ],
    "availableTags": []
}</pre>



<h2 class="wp-block-heading" id="h-create-datadog-account">Create Datadog Account</h2>



<p>Excellent! At this point, we are sure that our Spring Boot app collects metrics, so it&#8217;s time to proceed with the Datadog part. </p>



<p>If you haven&#8217;t done it yet, then let&#8217;s create a trial account together. </p>



<p>Let&#8217;s navigate to the <a href="https://www.datadoghq.com/">https://www.datadoghq.com/</a> and click the &#8220;Free trial&#8221; button:</p>



<figure class="wp-block-image aligncenter size-large"><img decoding="async" width="1024" height="456" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_free_trial_2-1024x456.png" alt="Screenshot presents the main page of Datadog site with free trial button." class="wp-image-11009071" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_free_trial_2-1024x456.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_free_trial_2-300x134.png 300w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_free_trial_2-768x342.png 768w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_free_trial_2.png 1397w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Following, let&#8217;s fill out the signup form: </p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="532" height="867" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_sign_up_form_3_1.png" alt="Image is a screenshot of the Datadog signup form" class="wp-image-11009072" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_sign_up_form_3_1.png 532w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_sign_up_form_3_1-184x300.png 184w" sizes="auto, (max-width: 532px) 100vw, 532px" /></figure>



<p>And with that done, when we confirm our e-mail address, we will land on this page: </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="960" height="1006" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_key_4.png" alt="Screenshot presents the Datadog agent setup page. " class="wp-image-11009073" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_key_4.png 960w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_key_4-286x300.png 286w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_key_4-768x805.png 768w" sizes="auto, (max-width: 960px) 100vw, 960px" /></figure>



<p>As we can see, the above page contains a bunch of guides on how to set up the agent. </p>



<p>And although we are not interested in any of those, this page has one, valuable information for us- the <strong>API key</strong>. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Note:</strong> I think that readers of this blog are well aware of that, but for my own peace of mind- API keys are vulnerable data, so you <strong>must be cautious</strong> with them! 🙂 </p>
</blockquote>



<p>At this point, please copy this value and we will use it in the proceeding steps. </p>



<h2 class="wp-block-heading" id="h-configure-app-to-send-metrics">Configure App To Send Metrics</h2>



<p>When we have our Datadog account ready, we can finally configure our app. </p>



<h3 class="wp-block-heading" id="h-api-key">API Key</h3>



<p>Firstly, let&#8217;s get back to our Spring Boot project and update the <code>application.yaml</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health
  datadog:
    metrics:
      export:
        api-key: YOUR_KEY_VALUE</pre>



<p>Of course, we must replace the <code>YOUR_KEY_VALUE</code> with the actual API key. </p>



<h3 class="wp-block-heading" id="h-update-datadog-uri">Update Datadog URI </h3>



<p>If you are using Datadog US (the option you could select when signing up), then that&#8217;s pretty much it and you can restart the application.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>By default, metrics are sent to the Datadog US site (api.datadoghq.com). If your Datadog project is hosted on one of the other sites, or you need to send metrics through a proxy, configure the URI accordingly.</p>
</blockquote>



<p>So given the above, if we use another region, then we need to add one more line: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health
  datadog:
    metrics:
      export:
        api-key: YOUR_KEY_VALUE 
        uri: https://api.datadoghq.eu</pre>



<h3 class="wp-block-heading" id="h-update-metrics-upload-interval">Update Metrics Upload Interval</h3>



<p>Following, let&#8217;s see how easily we can update the interval, in which data are sent to Datadog: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health
  datadog:
    metrics:
      export:
        api-key: YOUR_KEY_VALUE 
        uri: https://api.datadoghq.eu
        step: 10s</pre>



<p>The default value is <strong>30 seconds</strong>. And to change that, the only thing we need to do is to add the <code>step</code> value. </p>



<h3 class="wp-block-heading" id="h-set-spring-logging-level-to-debug">Set Spring Logging Level to DEBUG</h3>



<p>As the last thing, which technically is not directly related to sending metrics, I would like to add the following to the root of our <code>application.yaml</code> file: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">logging:
  level:
    root: DEBUG</pre>



<p>This way we set the logging level in our Spring Boot application to <code>DEBUG</code>. </p>



<p>Why? It will be useful in a moment when we will be testing our implementation. </p>



<h2 class="wp-block-heading" id="h-testing">Testing </h2>



<p>With all of that done, we can finally verify that everything is working properly. </p>



<h3 class="wp-block-heading" id="h-verify-spring-boot-sends-data-to-datadog">Verify Spring Boot Sends Data to Datadog</h3>



<p>Firstly, let&#8217;s rerun our application and take a look at the logs.</p>



<p>If we see the following: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">i.m.datadog.DatadogMeterRegistry         : successfully sent 78 metrics to datadog</pre>



<p>Then data are successfully sent to the Datadog API. </p>



<p>Let&#8217;s leave it running for some time (like 2 minutes, or something), so that we have actual data later to work with 🙂 </p>



<h2 class="wp-block-heading" id="h-review-metrics-in-datadog">Review Metrics in Datadog </h2>



<p>After that time, the only thing left is to take a look at metrics in Datadog. </p>



<p>If we haven&#8217;t closed the Datadog &#8220;Agent Setup&#8221; page, then it should automatically close and redirect us to the dashboard. </p>



<p>However, if that didn&#8217;t happen, then no worries, we can simply navigate to the <a href="https://app.datadoghq.eu/metric/summary" target="_blank" rel="noreferrer noopener">metrics summary page</a>: </p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="733" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_summary_5-1024x733.png" alt="Screenshot presents the Datadog metrics summary page where all Spring Boot metrics are displayed." class="wp-image-11009076" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_summary_5-1024x733.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_summary_5-300x215.png 300w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_summary_5-768x550.png 768w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_summary_5.png 1350w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>On this page, we can see all the metrics that have been gathered so far. Long story short, this page contains meta-information about metrics themselves, like tags, etc. </p>



<p>I strongly recommend you spend here some time and figure out different options. </p>



<p>And if we would like to take a look at the actual values, then let&#8217;s navigate to the <a href="https://app.datadoghq.eu/metric/explorer" target="_blank" rel="noreferrer noopener">metrics explorer</a>: </p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="249" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-1024x249.png" alt="Screenshot presents Datadog metrics explorer with jvm memory used metric chart." class="wp-image-11009077" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-1024x249.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-300x73.png 300w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-768x187.png 768w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-1536x373.png 1536w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_6-2048x498.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>I know, the image is quite small (the &#8220;Open Image in New Tab&#8221; option will help here 😉 ). However, the only thing we need to do here is to specify the metric name in the left upper part and the period we would like to view in the right upper part. </p>



<p>After that, we will see the above chart. </p>



<p>And basically, that&#8217;s how metrics work with Spring Boot and Datadog, but please don&#8217;t close this article yet, I wanted to show you two, interesting things that may be useful to you in real life. </p>



<h2 class="wp-block-heading" id="h-sending-custom-tags">Sending Custom Tags </h2>



<p><strong>Tags</strong> are nothing else, than additional metadata you can send with metrics.  </p>



<p>Why are they useful? Well, thanks to them we can later differentiate different metrics. For example, we can send a tag with the application name, so that later we will group metrics by microservices, or environment value, like dev, staging, or prod. </p>



<p>In Spring Boot, we have several ways to achieve that. And probably the easiest way is to (again) update the <code>application.yaml</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health
  datadog:
    metrics:
      export:
        api-key: YOUR_KEY_VALUE 
        uri: https://api.datadoghq.eu
        step: 10s
  metrics:
    tags:
      application: "my-app-one"</pre>



<p>As we can see, we can add a custom tag by using  <code>metrics.tags</code> and specifying that as a key-value pair. In our case- the <code>application</code> tag has the <code>my-app-one</code> value. </p>



<p>This approach is not only straightforward but also quite flexible. We could always put a placeholder for the environment variable instead of the hardcoded value. </p>



<p>Anyway, when we rerun our application and let Spring send some metrics to the Datadog, we will see that from now on we can <strong>filter values using the &#8220;from&#8221; field:</strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="281" src="http://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-1024x281.png" alt="" class="wp-image-11009080" srcset="https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-1024x281.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-300x82.png 300w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-768x210.png 768w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-1536x421.png 1536w, https://blog.codersee.com/wp-content/uploads/2024/08/spring_boot_datadog_metrics_explorer_custom_tag_7-2048x561.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Again, the &#8220;Open Image in New Tab&#8221; may be helpful here 😉 </p>



<h2 class="wp-block-heading" id="h-turn-off-metrics-export">Turn Off Metrics Export</h2>



<p>As the last thing, let&#8217;s take a look at how we can instruct our Spring app to <strong>stop sending metrics to Datadog</strong>.</p>



<p>Again, let&#8217;s navigate to the <code>application.yaml</code> file and set the <code>enabled</code> flag to false: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">management:
  endpoints:
    web:
      exposure:
        include: metrics, health
  datadog:
    metrics:
      export:
        enabled: false
        api-key: YOURE_KEY_VALUE
        uri: https://api.datadoghq.eu
        step: 10s</pre>



<p>Of course, we can use an environment variable placeholder (like <code>${DATADOG_ENABLED}</code>) and turn on/off export in different environments. </p>



<h2 class="wp-block-heading" id="h-summary">Summary </h2>



<p>That&#8217;s all for this tutorial on <strong>how to send Spring Boot metrics to Datadog</strong>. </p>



<p>As always, you can find the source code in <a href="https://github.com/codersee-blog/spring-boot-3-kotlin-datadog" target="_blank" rel="noreferrer noopener">this GitHub repository</a>.</p>



<p>Have a great day and don&#8217;t forget to <strong>leave a comment</strong>! 😀</p>
<p>The post <a href="https://blog.codersee.com/sending-spring-boot-metrics-datadog/">Sending Spring Boot Metrics to Datadog</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/sending-spring-boot-metrics-datadog/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Integration Tests for WebClient with WireMock</title>
		<link>https://blog.codersee.com/integration-tests-webclient-wiremock/</link>
					<comments>https://blog.codersee.com/integration-tests-webclient-wiremock/#comments</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Wed, 28 Feb 2024 10:56:57 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[JUnit 5]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[WebClient]]></category>
		<category><![CDATA[WireMock]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=9508747</guid>

					<description><![CDATA[<p>Learn how to perform integration testing for Spring WebClient that invokes external REST APIs with WireMock and JUnit 5.</p>
<p>The post <a href="https://blog.codersee.com/integration-tests-webclient-wiremock/">Integration Tests for WebClient with WireMock</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hi guys! 🙂 Today we will learn how to do <strong>integration testing</strong> for <strong>Spring WebClient</strong> that invokes external REST APIs with <strong>WireMock</strong> and <strong>JUnit 5</strong>. </p>



<p>As an example, we will implement a simple logic responsible for querying the <strong>GitHub API</strong> using coroutines. Nevertheless, I am pretty sure you will be able to easily adjust this article to your needs. (of course, if you are here only for the WireMock part, then I recommend skipping to the &#8220;WebClient Integration Testing With WireMock&#8221; chapter).</p>



<p>Lastly, I just wanted to add that you can use this testing approach regardless of whether you are using WebClient, Retrofit, Ktor Client, or any other HTTP client. </p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<a href="https://blog.codersee.com/integration-tests-webclient-wiremock/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2Fhbr4snySA6I%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p></div>



<p>If you find this content useful,<strong> please leave a subscription</strong> 🙂</p>



<h2 class="wp-block-heading" id="h-which-webclient-testing-strategy-is-the-best">Which WebClient Testing Strategy Is The Best?</h2>



<p>But before we head to the practice part, let&#8217;s try to answer the above question. </p>



<p>Quick answer- there is no such thing as <strong>the best testing strategy</strong>. </p>



<p>And for the longer answer, let&#8217;s take a look at the visualization of the testing pyramid first:</p>



<figure class="wp-block-image aligncenter size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="1024" src="http://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid-1024x1024.png" alt="Image presents a diagram with testing pyramid including e2e tests, integration tests, and unit tests helping to better understand the best approach for WebClient WireMock testing. " class="wp-image-9508749" style="width:542px;height:auto" srcset="https://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid-1024x1024.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid-300x300.png 300w, https://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid-150x150.png 150w, https://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid-768x768.png 768w, https://blog.codersee.com/wp-content/uploads/2024/02/tests_pyramid.png 1080w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>The tests are ordered based on their scope and execution time. The closer we are to the top, the longer the tests take, but they also give us more confidence. </p>



<ul class="wp-block-list">
<li><strong>unit tests</strong> form the base, as they are quick to run and pinpoint specific code issues,</li>



<li><strong>integration tests</strong> come next, validating the interactions between units,</li>



<li><strong>E2E tests</strong>, with a broader scope, are placed at the top.</li>
</ul>



<p>So long story short, although the e2e tests give us the most confidence, we cannot rely only on them, because the feedback loop would take way too long. And this would affect the development process.</p>



<p>So usually, we make concessions and introduce more integration and unit tests. </p>



<p>But which of these two should we pick for the WebClient? </p>



<p>In my opinion- <strong>integration tests</strong>. </p>



<p>Whenever we are talking about logic responsible for communicating with external API, we must make sure that: </p>



<ul class="wp-block-list">
<li>we query the appropriate URL, </li>



<li>we pass the necessary headers with appropriate values, </li>



<li>request/response bodies are properly serialized/deserialized, </li>



<li>and many, many more. </li>
</ul>



<p>With unit testing, the amount of mocking here would be so significant that I would question whether we need such a test at all. </p>



<p>Moreover, in Spring we can write integration tests for WebClient with WireMock at almost no effort. </p>



<h2 class="wp-block-heading" id="h-set-up-github-api-key">Set Up GitHub API Key </h2>



<p>With that said, let&#8217;s set up the API key we will use to query the GitHub API. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite><strong>Note:</strong> technically, the endpoint we will use does not require <strong>any token.</strong> However, for the learning purposes I want to show you how to deal with passing bearer tokens, too 🙂 </cite></blockquote>



<p>As the first step, let&#8217;s navigate to the <a href="https://github.com/settings/tokens" target="_blank" rel="noreferrer noopener">tokens tab</a> in GitHub settings and click the &#8220;Generate new token (classic)&#8221;: </p>



<figure class="wp-block-image aligncenter size-full is-resized"><img loading="lazy" decoding="async" width="777" height="190" src="http://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_1.png" alt="Image is a screenshot and presents how to generate new classic GitHub token." class="wp-image-9508752" style="width:777px;height:auto" srcset="https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_1.png 777w, https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_1-300x73.png 300w, https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_1-768x188.png 768w" sizes="auto, (max-width: 777px) 100vw, 777px" /></figure>



<p>On the next page, let&#8217;s specify the name, expiration, and desired scopes: </p>



<figure class="wp-block-image aligncenter size-full"><img loading="lazy" decoding="async" width="780" height="457" src="http://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_2.png" alt="Image is a screenshot from GitHub tokens page." class="wp-image-9508753" srcset="https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_2.png 780w, https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_2-300x176.png 300w, https://blog.codersee.com/wp-content/uploads/2024/02/github_generate_token_2-768x450.png 768w" sizes="auto, (max-width: 780px) 100vw, 780px" /></figure>



<p></p>



<p>If you don&#8217;t know what scopes you will need, please follow the principle of lowest privilege. At the end of the day, we can generate a new token at some point in the future when the requirements change.</p>



<p>When we are ready, let&#8217;s hit the generate button <strong>copy the token value</strong>, and <strong>store it securely</strong>. We will not see it again! </p>



<h2 class="wp-block-heading" id="h-query-the-api-with-webclient">Query The Api With WebClient</h2>



<p>Before we implement the WireMock tests, let&#8217;s prepare a simple project responsible for querying the external API with WebClient. So, even if you are here only for the WireMock part, I encourage you to briefly check this paragraph to be on the same page 😉 </p>



<p>Long story short, we will implement WebClient with coroutines that will query the &#8220;List repositories for a user&#8221; endpoint. For more details about it, check out the <a href="https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user" target="_blank" rel="noreferrer noopener">GitHub API documentation</a>, please. </p>



<h3 class="wp-block-heading" id="h-generate-new-project">Generate New Project</h3>



<p>As the first step, let&#8217;s navigate to the <a href="https://start.spring.io/" target="_blank" rel="noreferrer noopener">https://start.spring.io/</a> page and generate a fresh Spring Boot project:</p>



<figure class="wp-block-image aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="530" src="http://blog.codersee.com/wp-content/uploads/2024/02/webclient_wiremock_generate_fresh_spring_boot_project-1024x530.png" alt="Image presents the Spring Initializr page screenshot of a project setting that we will use to implement WebClient logic and test with WireMock." class="wp-image-9508754" srcset="https://blog.codersee.com/wp-content/uploads/2024/02/webclient_wiremock_generate_fresh_spring_boot_project-1024x530.png 1024w, https://blog.codersee.com/wp-content/uploads/2024/02/webclient_wiremock_generate_fresh_spring_boot_project-300x155.png 300w, https://blog.codersee.com/wp-content/uploads/2024/02/webclient_wiremock_generate_fresh_spring_boot_project-768x398.png 768w, https://blog.codersee.com/wp-content/uploads/2024/02/webclient_wiremock_generate_fresh_spring_boot_project.png 1510w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>In general, the most important thing is that we will use Kotlin and Spring Reactive Web this time. Versions and metadata are totally up to you. </p>



<p>Of course, let&#8217;s click the &#8220;Generate&#8221; button, download the ZIP file, and import that to our IDE. Personally, I am using IntelliJ IDEA and if you would like to learn more about its setup, then check out my video on <a href="https://www.youtube.com/watch?v=npib90rBLE4" target="_blank" rel="noreferrer noopener">how to install IntelliJ for Kotlin development</a>.</p>



<h3 class="wp-block-heading" id="h-update-application-properties">Update Application Properties</h3>



<p>As the next step, let&#8217;s navigate to the <code>resources</code> package and update the <code>application.yaml</code> file: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">api:
  github:
    key: ${GITHUB_API_TOKEN:123}
    url: ${GITHUB_API_BASE_URL:http://localhost:8090}
    version: ${GITHUB_API_VERSION:2022-11-28}</pre>



<p>Above we can see our custom properties. </p>



<p>We will source values for our GitHub key, URL, and version from environment variables. </p>



<p>Not only is this approach <strong>secure</strong> (hardcoding the key with token value would be the worst thing you could do), but also <strong>flexible</strong> (we can pass different values depending on the environment we are running our app in). </p>



<h3 class="wp-block-heading" id="h-webclient-configuration">WebClient Configuration</h3>



<p>Following, let&#8217;s configure a WebClient bean that we will use whenever we want to call the GitHub API. </p>



<p>Firstly, let&#8217;s add the <code>config</code> package and put the <code>GitHubApiProperties</code> data class in there:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@ConfigurationProperties("api.github")
data class GitHubApiProperties(
    val url: String,
    val key: String,
    val version: String,
)</pre>



<p>In my opinion, the <code>@ConfigurationProperties</code> annotation is the cleanest way to inject values from properties. As we can see above, the <strong>prefix</strong> and <strong>field names</strong> simply reflect the structure from the previous step. </p>



<p>With that done, let&#8217;s add the <code>GitHubApiConfig</code> in the same package: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Configuration
@EnableConfigurationProperties(GitHubApiProperties::class)
class GitHubApiConfig(
    private val gitHubApiProperties: GitHubApiProperties,
) {
    @Bean
    fun webClient(builder: WebClient.Builder): WebClient =
        builder
            .baseUrl(gitHubApiProperties.url)
            .defaultHeader("Authorization", "Bearer ${gitHubApiProperties.key}")
            .defaultHeader("X-GitHub-Api-Version", gitHubApiProperties.version)
            .defaultHeader("Accept", "application/vnd.github+json")
            .build()
}</pre>



<p>Quite a few things are happening here, so let&#8217;s dive into each one. </p>



<p>Firstly, we annotate the class with <code>@Configuration</code> and <code>@EnableConfigurationproperties</code>. The second annotation is necessary to for the previous steps to work. </p>



<p>Then, we inject the <code>GitHubApiProperties</code> instance and use its values to configure a new WebClient bean. This configuration is quite straightforward. We set the base URL and a bunch of headers that will be sent with <strong>every request</strong>: the authorization token, GH API version, and the accept header (those two are specific to the GitHub API, so let&#8217;s focus on that too much).  </p>



<h3 class="wp-block-heading" id="h-prepare-model-classes">Prepare Model Classes</h3>



<p>With that done, let&#8217;s prepare a bunch of data classes that we will need to deserialize JSON responses from the API (+ one exception class). If you would like to add other fields, please check out the documentation link I shared with you before. </p>



<p>As the first step, let&#8217;s add the <code>model</code> package and the <code>PageableGitHubResponse</code>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">data class PageableGitHubResponse&lt;T>(
    val items: List&lt;T>,
    val hasMoreItems: Boolean,
)</pre>



<p>The API endpoints allow us to get only a chunk of data using the <code>page</code> and <code>per_page</code> parameters. And this class can be reused whenever we&#8217;re using a paginated endpoint. </p>



<p>As the next step, let&#8217;s implement the <code>GitHubRepoResponse</code> along with <code>GitHubOwnerResponse</code>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">data class GitHubRepoResponse(
    val fork: Boolean,
    val name: String,
    val owner: GitHubOwnerResponse,
)

data class GitHubOwnerResponse(
    val login: String,
)</pre>



<p>As I mentioned at the beginning of this paragraph, we need those two in order to deserialize the actual JSON response payload. </p>



<p>Lastly, let&#8217;s add the <code>UpstreamApiException</code>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import org.springframework.http.HttpStatusCode

data class UpstreamApiException(
    val msg: String,
    val statusCode: HttpStatusCode,
) : RuntimeException(msg)</pre>



<p>This way, we will be able to throw custom exceptions. Such an exception could be handled later, for example with <a href="https://blog.codersee.com/exception-handling-with-restcontrolleradvice-and-exceptionhandler/" target="_blank" rel="noreferrer noopener">@RestControllerAdvice</a>.</p>



<h3 class="wp-block-heading" id="h-make-use-of-webclient">Make Use Of WebClient</h3>



<p>Finally, let&#8217;s combine all of that together. </p>



<p>So, let&#8217;s add the <code>api</code> package and implement the <code>GitHubApi</code> class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Component
class GitHubApi(
  private val webClient: WebClient,
) {

  suspend fun listRepositoriesByUsername(
    username: String,
    page: Int,
    perPage: Int,
  ): PageableGitHubResponse&lt;GitHubRepoResponse>? =
    webClient.get()
      .uri("/users/$username/repos?page=$page&amp;per_page=$perPage")
      .awaitExchangeOrNull(::mapToPageableResponse)
}</pre>



<p>Don&#8217;t worry about the <code>mapToPageableResponse</code>, we will add it in a moment. </p>



<p>But before that, let&#8217;s analyze the above code. </p>



<p>Firstly, we annotate the class as the <code>@Component</code> to make it a Spring bean and inject the <code>WebClient</code> instance we configured earlier. We don&#8217;t have other WebClient instances, so there is no need to use <code>@Qualifier</code> or anything like that. </p>



<p>Following, we add the <code>listRepositoriesByUsername</code> function that lets us pass the <code>username</code> along with <code>page</code> and <code>perPage</code>. This way, we make this function reusable and allow the invoker to decide how to tackle the pagination. </p>



<p>Additionally, we mark it as a suspend function because the <code>awaitExchangeOrNull</code> is a suspend function. However, if you are interested in this topic, then I refer you to my other article about <a href="https://blog.codersee.com/spring-webclient-with-kotlin-coroutines/" target="_blank" rel="noreferrer noopener">WebClient with coroutines</a>. </p>



<p>Lastly, let&#8217;s add the missing code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private suspend inline fun &lt;reified T> mapToPageableResponse(clientResponse: ClientResponse): PageableGitHubResponse&lt;T>? {
  val hasNext = checkIfMorePagesToFetch(clientResponse)

  return when (val statusCode = clientResponse.statusCode()) {
    HttpStatus.OK ->
      PageableGitHubResponse(
        items = clientResponse.awaitBody&lt;List&lt;T>>(),
        hasMoreItems = hasNext,
      )

    HttpStatus.NOT_FOUND -> null

    else -> throw UpstreamApiException(
      msg = "GitHub API request failed.",
      statusCode = statusCode,
    )
  }
}

fun checkIfMorePagesToFetch(clientResponse: ClientResponse) =
  clientResponse.headers()
    .header("link")
    .firstOrNull()
    ?.contains("next")
    ?: false</pre>



<p>Long story short, the above code works as follows: </p>



<ol class="wp-block-list">
<li>Firstly, we take the response and check if there are more pages. How? Well, without going into details, GitHub API returns the <code>link</code> header. And if its value contains the <code>rel="next"</code>, it means that there are more pages to fetch. </li>



<li>Finally, we check the status code. If it is <code>200 OK</code>, we simply read the JSON value. When we get <code>404 Not Found</code>, we return null (we can expect this status code when we pass a non-existing username, and in my opinion, this case is not exceptional). And whenever we receive another status code, we simply throw the <code>UpstreamApiException</code>.</li>
</ol>



<h2 class="wp-block-heading" id="h-webclient-integration-testing-with-wiremock">WebClient Integration Testing With WireMock</h2>



<p>Excellent, at this point we have (almost) everything we need to test our WebClient implementation with WireMock. </p>



<p>We will see only a few example test cases that you will be able to easily adapt according to your needs:</p>



<ul class="wp-block-list">
<li>200 OK response with an empty list, 200 OK with items, and more pages, and 200 OK with items and no more pages to fetch</li>



<li>404 Not Found, </li>



<li>401 Unauthorized</li>
</ul>



<p>When you will be implementing your own integration tests, this is the moment when you should do the homework and analyze the API you are working with. What are the possible happy paths? How should your code behave in case of any error status code? What if we add some latency? And many, many more 🙂 </p>



<h3 class="wp-block-heading" id="h-additional-dependencies">Additional Dependencies</h3>



<p>At this point, we defined our test cases, so we are ready to get our hands dirty. </p>



<p>So, let&#8217;s start by adding the necessary dependencies: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">testImplementation("org.springframework.cloud:spring-cloud-contract-wiremock:4.1.1")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")</pre>



<p>As we can see, the first one- the Spring Cloud Contract WireMock module- allows us to use WireMock in our app, whereas the <code>kotlinx-coroutines-test</code> is necessary due to our coroutines WebClient implementation.</p>



<h3 class="wp-block-heading" id="h-add-expected-json-files">Add Expected JSON Files</h3>



<p>So, if we know what test cases we would like to test, we should prepare the example JSON API responses. </p>



<p>Personally, whenever working with WireMock I am a big fan of externalizing the expected JSON files. We put them inside the <code>/test/resources</code> directory and refer to them later in the tests. This way, we can improve the readability of the test classes. Of course, <strong>unless we want to prepare JSON responses dynamically</strong>. </p>



<p>In our case, we could simply invoke the GitHub API and persist responses for different cases. This way, we prepare the following files and put them inside the <code>/test/resources/responses/external/github</code>: </p>



<ul class="wp-block-list">
<li></li>
</ul>



<ul class="wp-block-list">
<li>list_github_repositories_200_OK_empty_list.json</li>



<li>list_github_repositories_200_OK_page_1.json</li>



<li>list_github_repositories_401_UNAUTHORIZED.json</li>



<li>list_github_repositories_404_NOT_FOUND.json</li>
</ul>



<p>You can find all of these files in my GH repo <a href="https://github.com/codersee-blog/integration-testing-spring-webflux-webclient-wiremock/tree/main/src/test/resources/responses/external/github" target="_blank" rel="noreferrer noopener">here</a>. </p>



<p>The example for <code>401 Unauthorized</code> looks as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "message": "Bad credentials",
  "documentation_url": "https://docs.github.com/rest"
}</pre>



<h3 class="wp-block-heading" id="h-prepare-util-function">Prepare Util Function</h3>



<p>As the last thing before we implement our test class, let&#8217;s add the util function inside the <code>util</code> package (of course, in tests). </p>



<p>This function will be responsible for reading JSON files inside the test resources as String values:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import org.springframework.core.io.ClassPathResource

fun getResponseBodyAsString(path: String): String =
    ClassPathResource(path).getContentAsString(
        Charsets.UTF_8,
    )</pre>



<h3 class="wp-block-heading" id="h-implement-githubapitest-class">Implement GitHubApiTest Class</h3>



<p>Finally, let&#8217;s start implementing the <code>GitHubApiTest</code> class: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private const val TEST_KEY = "TEST_KEY"
private const val TEST_PORT = 8082
private const val TEST_VERSION = "2022-11-28"

@AutoConfigureWireMock(port = TEST_PORT)
@TestPropertySource(
  properties = [
    "api.github.url=http://localhost:${TEST_PORT}",
    "api.github.key=$TEST_KEY",
    "api.github.version=$TEST_VERSION",
  ],
)
@SpringBootTest(
  webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
)
class GitHubApiTest {
  @Autowired
  private lateinit var wireMockServer: WireMockServer

  @Autowired
  private lateinit var gitHubApi: GitHubApi

  private val page = 1
  private val perPage = 2
  private val username = UUID.randomUUID().toString()

}</pre>



<p>Conceptually, everything starts with the <code>@SpringBootTest</code> and <code>@AutoConfigureWireMock</code> annotations.</p>



<p>We use the <code>@SpringBootTest</code> annotation in Spring Boot integration tests. This way a new ApplicationContext will be created. In our case, we set the <code>webEnvironment</code> with <code>RANDOM_PORT</code> value, so that a reactive context listening on the random port is created. </p>



<p>Additionally, we combine that together with <code>@AutoConfigureWireMock</code> and fixed test port. This way a WireMock server will be started as a part of Application Context. </p>



<p>Lastly, we do 3 things: </p>



<ol class="wp-block-list">
<li>We use the <code>@TestPropertySource</code> to pass values for properties.</li>



<li>We inject the <code>WireMockServer</code> and <code>GitHubApi</code> instances that we will work with later. Please note that we can do that just like we would do with a standard, non-test, Spring component. </li>



<li>We prepare some dummy data for <code>page</code>, <code>perPage</code>, and <code>username</code> values we will need for testing. </li>
</ol>



<p>As a note of comment from my side- this &#8220;skeleton&#8221; is useful whenever working with WebClient and WireMock.</p>



<h3 class="wp-block-heading" id="h-test-404-not-found-api-response-case">Test 404 Not Found API Response Case</h3>



<p>Wonderful! </p>



<p>As the first test, let&#8217;s start with the <code>404 Not Found</code> case. Usually, we will start with happy path cases, but for learning purposes, this one is the shortest case 😉 </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Test
fun `Given 404 NOT FOUND response When fetching repository by username Then should return null`() = runTest {
  // Given
  wireMockServer.stubFor(
    WireMock.get(WireMock.urlEqualTo("/users/$username/repos?page=$page&amp;per_page=$perPage"))
      .withHeader("Authorization", WireMock.equalTo("Bearer $TEST_KEY"))
      .withHeader("X-GitHub-Api-Version", WireMock.equalTo(TEST_VERSION))
      .withHeader("Accept", WireMock.equalTo("application/vnd.github+json"))
      .willReturn(
        WireMock.aResponse()
          .withStatus(HttpStatus.NOT_FOUND.value())
          .withHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
          .withBody(
            getResponseBodyAsString("/responses/external/github/list_github_repositories_404_NOT_FOUND.json"),
          ),
      ),
  )

  // When
  val result = gitHubApi.listRepositoriesByUsername(username, page, perPage)

  // Then
  Assertions.assertNull(result)
}</pre>



<p>We mark our test with the <code>@Test</code> (JUnit 5), specify the meaningful name for the test (with awesome Kotlin backticks), and wrap our test with <code>runTest</code>. The last one is necessary as we are working with coroutines and invoke the suspended function. Long story short, in JVM it will behave just like <code>runBlocking</code> (but it will skip delays). </p>



<p>Next comes the WireMock part. And to be even more specific- <strong>stubbing.</strong> </p>



<p>Stubbing is nothing else than &#8220;the ability to return canned HTTP responses for requests matching criteria&#8221; (from docs). Simply said, whenever an outgoing request that matches our criteria is made, a mocked response configured in <code>willReturn</code> is returned. </p>



<p>So, as we can see, the above logic will work only when: </p>



<ul class="wp-block-list">
<li>the GET request is made, </li>



<li>URL matches, </li>



<li>Headers sent have appropriate values </li>
</ul>



<p>And this part <strong>already tests our logic.</strong> Pretty neat, isn&#8217;t it? 🙂 </p>



<p>Lastly, we simply invoke the function and assert that <code>null</code> is returned. Which confirms that everything is working fine. </p>



<h3 class="wp-block-heading" id="h-test-401-unauthorized">Test 401 Unauthorized </h3>



<p>As we already started with error cases, let&#8217;s verify that whenever the endpoint returns <code>401 Unauthorized</code>, the <code>UpstreamApiException</code> is thrown:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Test
fun `Given 401 UAUTHORIZED response When fetching repository by username Then should throw UpstreamApiException`() = runTest {
  // Given
  wireMockServer.stubFor(
    WireMock.get(WireMock.urlEqualTo("/users/$username/repos?page=$page&amp;per_page=$perPage"))
      .withHeader("Authorization", WireMock.equalTo("Bearer $TEST_KEY"))
      .withHeader("X-GitHub-Api-Version", WireMock.equalTo(TEST_VERSION))
      .withHeader("Accept", WireMock.equalTo("application/vnd.github+json"))
      .willReturn(
        WireMock.aResponse()
          .withStatus(HttpStatus.UNAUTHORIZED.value())
          .withHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
          .withBody(
            getResponseBodyAsString("/responses/external/github/list_github_repositories_401_UNAUTHORIZED.json"),
          ),
      ),
  )

  // When
  val exception = assertThrows&lt;UpstreamApiException> {
    gitHubApi.listRepositoriesByUsername(username, page, perPage)
  }

  // Then
  assertNotNull(exception)

  assertEquals("GitHub API request failed.", exception.msg)
  assertEquals(HttpStatus.UNAUTHORIZED, exception.statusCode)
}</pre>



<p>As we can see, the logic responsible for stubbing only slightly changed. </p>



<p>The interesting part here is the <code>assertThrows</code>, which not only makes sure that the exception of an appropriate type is thrown but also returns it. This way we can make additional assertions.</p>



<h3 class="wp-block-heading" id="h-verify-200-ok-with-empty-list">Verify 200 OK With Empty List</h3>



<p>Following, let&#8217;s cover the empty list case:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Test
fun `Given 200 OK response with empty list When fetching repository by username Then should return repository with correct properties and has next false`() =
  runTest {
    // Given
    val linkHeader = """&lt;https://api.github.com/user/64011387/repos?page=3&amp;per_page=2>; rel="prev","""

    wireMockServer.stubFor(
      WireMock.get(WireMock.urlEqualTo("/users/$username/repos?page=$page&amp;per_page=$perPage"))
        .withHeader("Authorization", WireMock.equalTo("Bearer $TEST_KEY"))
        .withHeader("X-GitHub-Api-Version", WireMock.equalTo(TEST_VERSION))
        .withHeader("Accept", WireMock.equalTo("application/vnd.github+json"))
        .willReturn(
          WireMock.aResponse()
            .withStatus(HttpStatus.OK.value())
            .withHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
            .withHeader(HttpHeaders.LINK, linkHeader)
            .withBody(
              getResponseBodyAsString("/responses/external/github/list_github_repositories_200_OK_empty_list.json"),
            ),
        ),
    )

    // When
    val result = gitHubApi.listRepositoriesByUsername(username, page, perPage)

    // Then
    val expected =
      PageableGitHubResponse(
        items = emptyList&lt;GitHubRepoResponse>(),
        hasMoreItems = false,
      )

    assertEquals(expected, result)
  }</pre>



<p>This time, we assign the returned value to the <code>result</code> and compare that with the <code>expected</code>. </p>



<h3 class="wp-block-heading" id="h-check-200-ok-with-has-next-true">Check 200 OK With Has Next True</h3>



<p>Last before least, let&#8217;s make sure that we got the expected objects in a list and that the logic responsible for the <code>link</code> header check returns true:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Test
fun `Given 200 OK response with payload When fetching repository by username Then should return repository with correct properties and has next true`() =
  runTest {
    // Given
    val linkHeader = """&lt;https://api.github.com/user/64011387/repos?page=3&amp;per_page=2>; rel="next","""

    wireMockServer.stubFor(
      WireMock.get(WireMock.urlEqualTo("/users/$username/repos?page=$page&amp;per_page=$perPage"))
        .withHeader("Authorization", WireMock.equalTo("Bearer $TEST_KEY"))
        .withHeader("X-GitHub-Api-Version", WireMock.equalTo(TEST_VERSION))
        .withHeader("Accept", WireMock.equalTo("application/vnd.github+json"))
        .willReturn(
          WireMock.aResponse()
            .withStatus(HttpStatus.OK.value())
            .withHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
            .withHeader(HttpHeaders.LINK, linkHeader)
            .withBody(
              getResponseBodyAsString("/responses/external/github/list_github_repositories_200_OK_page_1.json"),
            ),
        ),
    )

    // When
    val result = gitHubApi.listRepositoriesByUsername(username, page, perPage)

    // Then
    val expected =
      PageableGitHubResponse(
        items =
        listOf(
          GitHubRepoResponse(
            fork = false,
            name = "controlleradvice-vs-restcontrolleradvice",
            owner = GitHubOwnerResponse(login = "codersee-blog"),
          ),
          GitHubRepoResponse(
            fork = false,
            name = "freecodecamp-spring-boot-kotlin-excel",
            owner = GitHubOwnerResponse(login = "codersee-blog"),
          ),
        ),
        hasMoreItems = true,
      )

    assertEquals(expected, result)
  }</pre>



<h3 class="wp-block-heading" id="h-test-200-ok-with-has-next-false">Test 200 OK With Has Next False</h3>



<p>And finally, let&#8217;s double-check the flag logic:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Test
fun `Given 200 OK response with payload When fetching repository by username Then should return repository with correct properties and has next false`() =
  runTest {
    // Given
    val linkHeader = """&lt;https://api.github.com/user/64011387/repos?page=3&amp;per_page=2>; rel="prev","""

    wireMockServer.stubFor(
      WireMock.get(WireMock.urlEqualTo("/users/$username/repos?page=$page&amp;per_page=$perPage"))
        .withHeader("Authorization", WireMock.equalTo("Bearer $TEST_KEY"))
        .withHeader("X-GitHub-Api-Version", WireMock.equalTo(TEST_VERSION))
        .withHeader("Accept", WireMock.equalTo("application/vnd.github+json"))
        .willReturn(
          WireMock.aResponse()
            .withStatus(HttpStatus.OK.value())
            .withHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
            .withHeader(HttpHeaders.LINK, linkHeader)
            .withBody(
              getResponseBodyAsString("/responses/external/github/list_github_repositories_200_OK_page_1.json"),
            ),
        ),
    )

    // When
    val result = gitHubApi.listRepositoriesByUsername(username, page, perPage)

    // Then
    val expected =
      PageableGitHubResponse(
        items =
        listOf(
          GitHubRepoResponse(
            fork = false,
            name = "controlleradvice-vs-restcontrolleradvice",
            owner = GitHubOwnerResponse(login = "codersee-blog"),
          ),
          GitHubRepoResponse(
            fork = false,
            name = "freecodecamp-spring-boot-kotlin-excel",
            owner = GitHubOwnerResponse(login = "codersee-blog"),
          ),
        ),
        hasMoreItems = false,
      )

    assertEquals(expected, result)
  }</pre>



<h3 class="wp-block-heading" id="h-integration-tests-with-wiremock-and-webclient-summary">Integration Tests With WireMock And WebClient Summary</h3>



<p>And basically, that&#8217;s all for this tutorial on how to perform integration testing for Spring WebClient that invokes external REST APIs with WireMock and JUnit 5.</p>



<p>I hope you enjoyed this one and I recommend you check out my other articles related to <a href="https://blog.codersee.com/category/spring/">Spring Framework</a>.</p>
<p>The post <a href="https://blog.codersee.com/integration-tests-webclient-wiremock/">Integration Tests for WebClient with WireMock</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/integration-tests-webclient-wiremock/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Fail Spring Boot App on Missing Environment Variables</title>
		<link>https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/</link>
					<comments>https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Sat, 16 Sep 2023 05:43:54 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[@ConfigurationProperties]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=9007594</guid>

					<description><![CDATA[<p>In this article we will learn 4 ways to prevent Spring Boot app from starting when environment variable is missing. </p>
<p>The post <a href="https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/">Fail Spring Boot App on Missing Environment Variables</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hello and welcome to my blog🙂 If you&#8217;ve been working with <strong>@ConfigurationProperties</strong> and <strong>environment variables</strong> in Spring Boot, then you probably saw that by default, Spring Boot does not fail when the value is missing.</p>



<p>In this article, I will show you <strong>four ideas</strong> on how to solve this issue. </p>



<p>Please keep in mind that all of them are workarounds, and if you came up with something even better, then do not forget to share in the comments🙂</p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<p></p></div>


<a href="https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2F6hAGbp8I5ow%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<p>If you find this content useful,<strong> please leave a subscription&nbsp;</strong> 😉</p>



<h2 class="wp-block-heading" id="h-problem">Problem</h2>



<p>To be on the same page, let&#8217;s take a quick look at a simple Spring Boot configuration with @ConfigurationProperties.<br><br>Firstly, let&#8217;s navigate to the <code>application.yaml</code> file: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">example:
  some-property: ${SOME_VARIABLE}</pre>



<p>As we can see, we defined a new property inside our config file for which the value should be set from the environment variable named <code>SOME_VARIABLE</code>.<br><br>Following, let&#8217;s introduce the <em>@ConfigurationProperties</em> class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@ConfigurationProperties(prefix = "example")
data class ExampleConfigurationProperties(
  val someProperty: String
)</pre>



<p>With this code, we expect that Spring Framework will read all properties defined in the configuration file and inject values into the matching properties.<br></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>Note: Spring recognizes the kebab case (some-property) and matches it with the property using camel case (someProperty)</cite></blockquote>



<p><br>After that, we need to explicitly inform Spring about our configuration properties class: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Configuration
@EnableConfigurationProperties(ExampleConfigurationProperties::class)
class Config</pre>



<p>Lastly, we must add a class, which injects the <em>ExampleConfigurationProperties</em> and prints out the value of <em>someProperty:</em></p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Component
class SomeComponent(
  private val properties: ExampleConfigurationProperties
) {

  @PostConstruct
  fun init() {
    println(properties.someProperty)
  }

}</pre>



<p>Finally, when we run our application <strong>without the <em>SOME_VARIABLE</em> environment variable missing</strong>,<strong> </strong>we will see the following output: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">${SOME_VARIABLE}</pre>



<p>A bit counterintuitive, right? <br><br>It would seem pretty obvious, that if we declare <em>someProperty </em>as a non-nullable String and environment variable is missing, our Spring Boot app should fail to start. <br><br>Unfortunately, that&#8217;s not the case when using <em>@ConfigurationProperties</em> and Spring will default the String value to the name of placeholder. <br><br>Moreover, at the moment of writing, there is no built-in way we could enforce that. </p>



<h2 class="wp-block-heading" id="h-solution-with-spring-validation">Solution With Spring Validation</h2>



<p>As a first solution (or rather workaround), let&#8217;s add the <a href="https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation" target="_blank" rel="noreferrer noopener">spring-boot-starter-validation</a> to our project:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">implementation("org.springframework.boot:spring-boot-starter-validation")</pre>



<p>Nextly, let&#8217;s instruct Spring that it should default to the blank String value whenever our environment variable is missing:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">example:
  some-property: ${SOME_VARIABLE:}</pre>



<p>After that let&#8217;s modify our configuration properties class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import jakarta.validation.constraints.NotBlank
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.validation.annotation.Validated

@ConfigurationProperties(prefix = "example")
@Validated
data class ExampleConfigurationProperties(
  @field:NotBlank val someProperty: String
)</pre>



<p>As we can see, with a combination of <em>@Validated</em> and <em>@NotBlank</em>, we ask Spring Framework to check if the value is <strong>not null</strong> and contains at least <strong>one non-whitespace character</strong>.<br> </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>Note: in Kotlin, we must use the @field: to annotate Java field. <br><br>If you would like to learn this, and many more then check out my <a href="https://blog.codersee.com/kotlin-handbook-learn-through-practice-course/">Kotlin course</a>.</cite></blockquote>



<p><br>Finally, when we try to run our application, we will see the following:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'example' to com.codersee.properties.ExampleConfigurationProperties failed:

    Property: example.someProperty
    Value: ""
    Origin: class path resource [application.yaml] - 2:18
    Reason: must not be blank


Action:

Update your application's configuration


Process finished with exit code 1</pre>



<p>And maybe this solution is nott ideal, but it does its job. </p>



<p>And it is neat. Not only does the application not start, but also whenever the environment variable for our @ConfigurationProperties is missing, we will see a meaningful message that will speed up the debugging process.</p>



<h2 class="wp-block-heading" id="h-custom-applicationrunner-idea">Custom ApplicationRunner Idea</h2>



<p>Another solution for our problem does not require any additional dependencies added to our project. </p>



<p>Let&#8217;s take a look at the following implementation of <em>ApplicationRunner</em>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component

@Component
class MyApplicationRunner : ApplicationRunner {

  companion object {
    private val requiredVariables = setOf(
      "SOME_VARIABLE"
    )
  }

  override fun run(args: ApplicationArguments) {
    requiredVariables.forEach {
      System.getenv(it)
        ?: throw MissingEnvVariableException(it)
    }
  }

}

class MissingEnvVariableException(variableName: String) :
  RuntimeException("Application failed to start. Missing environment variable: $variableName")
</pre>



<p>This time, we introduce our set of required environment variables and check if they are present.</p>



<p>Whenever it&#8217;s missing, we throw the <em>MissingEnvVariableException </em>and prevent the app from starting:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">java.lang.IllegalStateException: Failed to execute ApplicationRunner
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:765) ~[spring-boot-3.1.3.jar:3.1.3]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:752) ~[spring-boot-3.1.3.jar:3.1.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) ~[spring-boot-3.1.3.jar:3.1.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.3.jar:3.1.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.3.jar:3.1.3]
	at com.codersee.properties.PropertiesApplicationKt.main(PropertiesApplication.kt:13) ~[main/:na]
Caused by: com.codersee.properties.MissingEnvVariableException: Application failed to start. Missing environment variable: SOME_VARIABLE
	at com.codersee.properties.MyApplicationRunner.run(MyApplicationRunner.kt:19) ~[main/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762) ~[spring-boot-3.1.3.jar:3.1.3]
	... 5 common frames omitted


Process finished with exit code 1</pre>



<p>This solution gives us a bit more control and allows us to customize the behavior better. </p>



<p>Would we like to log an error, or maybe send an alert request? No problem. With a few lines of code, we can easily achieve that. </p>



<p>Nevertheless, with this approach, we introduce a new, separate place in the code we need to remember every time we want to add a new environment variable. And this might be easily forgotten in bigger projects.</p>



<h2 class="wp-block-heading" id="h-fix-with-conditional">Fix With @Conditional </h2>



<p>As the next step, let&#8217;s take a look at the approach with <em>@Conditional</em> annotation, which slightly reduces the chance of forgetting about additional checks. </p>



<p>Firstly, let&#8217;s add a new class called <em>RequiredEnvVariablesCondition</em>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class RequiredEnvVariablesCondition : Condition {

  private val logger: Logger = LogManager.getLogger(this::class.java)

  companion object {
    private val requiredVariables = setOf(
      "SOME_VARIABLE"
    )
  }

  override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean {
    val isEnvVariableMissing = requiredVariables
      .any { envVariableName ->
        val isMissing = System.getenv(envVariableName) == null

        if (isMissing)
          logger.error("Variable $envVariableName is missing!")

        isMissing
      }

    return !isEnvVariableMissing
  }

}</pre>



<p>As we can see, this logic is pretty similar to what we&#8217;ve done previously. </p>



<p>Every time the environment variable is missing, we log an error message and return false from the overridden <em>matches</em> function. </p>



<p>So with that done, we can annotate our config class with <em>@Conditional</em>:  </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Conditional
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(ExampleConfigurationProperties::class)
@Conditional(RequiredEnvVariablesCondition::class)
class Config</pre>



<p>This time, we will get the following output if we try to run the app without passing the environment variable:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2023-09-14T07:20:58.813+02:00 ERROR 16188 --- [           main] c.c.p.RequiredEnvVariablesCondition      : Variable SOME_VARIABLE is missing!

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.codersee.properties.SomeComponent required a bean of type 'com.codersee.properties.ExampleConfigurationProperties' that could not be found.


Action:

Consider defining a bean of type 'com.codersee.properties.ExampleConfigurationProperties' in your configuration.


Process finished with exit code 1</pre>



<h2 class="wp-block-heading" id="h-bonus-use-value-instead">(Bonus) Use @Value Instead</h2>



<p>There&#8217;s no doubt that <em>@ConfigurationProperties</em> helps us write cleaner code and better organize configuration in our codebase. Moreover, we can effortlessly add new values to the project with this approach. </p>



<p>However, in some cases, we should consider using @Value annotation instead:   </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.codersee.properties

import jakarta.annotation.PostConstruct
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class SomeComponent(
  @Value("\${example.some-property}") private val someProperty: String
) {

  @PostConstruct
  fun init() {
    println(someProperty)
  }
}</pre>



<p>This time, we can completely get rid of <em>@Configuration</em> and <em>@ConfigurationProperties</em> classes.</p>



<p>Instead of injecting a whole configuration object, we focus on the desired config value. </p>



<p>And whenever it&#8217;s missing, we get the following output out of the box:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-09-14T07:16:43.532+02:00 ERROR 18412 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someComponent' defined in file: Unexpected exception during bean creation
...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'SOME_VARIABLE' in value "${SOME_VARIABLE}"
...
Process finished with exit code 1</pre>



<p>As we can see, our Spring Boot app won&#8217;t start if the environment variable is missing and we will get a meaningful message. </p>



<p>But again, we should use <em>@Value</em> wisely and most of the time <em>@ConfigurationProperties</em> can be a better approach. </p>



<h2 class="wp-block-heading" id="h-summary">Summary </h2>



<p>And that&#8217;s all for this article on how to fail a Spring Boot app on missing environment variables. </p>



<p>As I mentioned in the beginning- these are just a few ideas that came to my mind. If you figure out some other way, then please share them in the comments section below. </p>



<p>As always, you can find the source code in this GitHub repository. </p>



<p>Thank you for reading, and see you next time! 🙂 </p>
<p>The post <a href="https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/">Fail Spring Boot App on Missing Environment Variables</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/fail-spring-boot-app-on-missing-environment-variables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Spring Boot 3 With Kotlin DSL. REST API Without Annotations</title>
		<link>https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/</link>
					<comments>https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Tue, 28 Mar 2023 06:00:42 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Kotlin DSL]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=6004437</guid>

					<description><![CDATA[<p>In this step-by-step guide, we will learn how to expose a REST API using Spring Boot 3, Kotlin, bean definition, and router DSL.</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/">Spring Boot 3 With Kotlin DSL. REST API Without Annotations</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="h-1-introduction">1. Introduction</h2>



<p>Hello ! 🙂 Continuing the DSL topic, I would like to show you how to expose a <strong>REST API</strong> using <strong>Spring Boot 3</strong>, <strong>Kotlin</strong>, <strong>bean definition</strong>, and <strong>router DSL</strong>.</p>



<p>In my <a href="https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/" target="_blank" rel="noopener">previous article</a>, I showed you how the router DSL works and how to expose endpoints manually without needing any web-related annotations, like <em>@RequestMapping</em>,<em> @RestController</em>,<em> @RequestBody</em>, etc&#8230;</p>



<p>But is it possible to eliminate the annotations when defining beans? Yes, and in this article, I will prove to you that it&#8217;s possible to expose a REST API without any annotations, like @Component or any specialization.</p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<p></p></div>


<a href="https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2FqITPCIq1xxw%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<p>If you find this content useful,<strong> please leave a subscription&nbsp;</strong> 😉</p>



<h2 class="wp-block-heading" id="h-2-project-setup">2. Project Setup</h2>



<p>Firstly, let&#8217;s navigate to <a href="https://start.spring.io/" target="_blank" rel="noopener">https://start.spring.io/</a> and generate a new project:</p>



<figure class="wp-block-image aligncenter"><img loading="lazy" decoding="async" width="1024" height="1024" src="http://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin-1024x1024.png" alt="The image is a screenshot from the start.spring.io page, which shows the setting necessary in order to create a Spring Boot 3 project with Kotlin DSL." class="wp-image-6004441" srcset="https://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin-1024x1024.png 1024w, https://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin-300x300.png 300w, https://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin-150x150.png 150w, https://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin-768x768.png 768w, https://blog.codersee.com/wp-content/uploads/2023/03/spring_initializr_screenshot_spring_boot_3_kotlin.png 1261w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>As we can see, in order to expose a REST API with Spring Boot 3 and Kotlin DSL, we don&#8217;t have to add any additional dependencies. The only one is the Spring Web, which lets us build web applications.</p>



<p>Following, please import the project to your favorite IDE (like IntelliJ).</p>



<h2 class="wp-block-heading" id="h-3-add-models">3. Add Models</h2>



<p>Nextly, let&#8217;s create the <code class="EnlighterJSRAW" data-enlighter-language="raw">model</code> package and add 3 data classes to our application:</p>



<ul class="wp-block-list">
<li><code class="EnlighterJSRAW" data-enlighter-language="raw">User</code>, which will transfer the data between the app and the data source,</li>



<li><code class="EnlighterJSRAW" data-enlighter-language="raw">UserDTO</code>, which will be serialized to JSON responses,</li>



<li>and <code class="EnlighterJSRAW" data-enlighter-language="raw">ErrorResponse</code>, shipping information about backend errors.</li>
</ul>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// User.kt
data class User(
  val id: Long? = null,
  val email: String,
  val name: String,
  val age: Int
)

// UserDTO.kt
data class UserDTO(
  val email: String,
  val name: String,
  val age: Int
)

// ErrorResponse.kt
data class ErrorResponse(
  val message: String
)
</pre>



<h2 class="wp-block-heading" id="h-4-implement-a-repository">4. Implement a Repository</h2>



<p>As the next step, let&#8217;s create a <code class="EnlighterJSRAW" data-enlighter-language="raw">repository</code> package.</p>


<p>[elementor-template id=&#8221;9007393&#8243;]</p>



<h3 class="wp-block-heading" id="h-4-1-add-userrepository-interface">4.1 Add UserRepository Interface</h3>



<p>Following, let&#8217;s introduce a <code class="EnlighterJSRAW" data-enlighter-language="raw">UserRepository</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">interface UserRepository {
  fun create(user: User): User
  fun findAll(): List&lt;User>
  fun findById(id: Long): User?
  fun updateById(id: Long, user: User): User?
  fun deleteById(id: Long)
}
</pre>



<p>And although oftentimes I skip this part for the simplicity of my tutorials, you will later see that working with interfaces, rather than implementations gives us much more flexibility. And in real-life scenarios, we should consider using them.</p>



<h3 class="wp-block-heading" id="h-4-2-add-implementation">4.2 Add Implementation</h3>



<p>So nextly, let&#8217;s add the <code class="EnlighterJSRAW" data-enlighter-language="raw">UserCrudRepository</code> to the codebase:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class UserCrudRepository(
  private val dataSource: MutableMap&lt;Long, User>
) : UserRepository {

  override fun create(user: User): User {
    val lastId = this.dataSource.keys.max()
    val incrementedId = lastId + 1
    val updatedUser = user.copy(id = incrementedId)

    this.dataSource[incrementedId] = updatedUser

    return updatedUser
  }

  override fun findAll(): List&lt;User> =
    this.dataSource.values
      .toList()

  override fun findById(id: Long): User? =
    this.dataSource[id]

  override fun updateById(id: Long, user: User): User? =
    this.dataSource[id]
      ?.let { foundUser -> 
        val updatedUser = user.copy(id = foundUser.id)
        this.dataSource[id] = updatedUser
        updatedUser
      }

  override fun deleteById(id: Long) {
    this.dataSource.remove(id)
  }
}
</pre>



<p>As we can see, this class is just a dummy in-memory database, which will be operating on the <code class="EnlighterJSRAW" data-enlighter-language="raw">dataSource</code> map provided as a constructor argument.</p>



<h3 class="wp-block-heading" id="h-4-3-implement-datasource">4.3 Implement DataSource</h3>



<p>With that being done, let&#8217;s create a new object called <code class="EnlighterJSRAW" data-enlighter-language="raw">DataSource</code> (can go to the <code class="EnlighterJSRAW" data-enlighter-language="raw">config</code> package):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">object DataSource {

  val devDataSource: MutableMap&lt;Long, User> = mutableMapOf(
    1L to User(1L, "email-1@gmail.com", "Name 1", 22),
    2L to User(2L, "email-2@gmail.com", "Name 2", 43),
    3L to User(3L, "email-3@gmail.com", "Name 3", 26),
    4L to User(4L, "email-4@gmail.com", "Name 4", 50)
  )

  val prodDataSource: MutableMap&lt;Long, User> = mutableMapOf(
    1L to User(1L, "prod-email-1@gmail.com", "Name 1", 22),
    2L to User(2L, "prod-email-2@gmail.com", "Name 2", 43),
    3L to User(3L, "prod-email-3@gmail.com", "Name 3", 26),
    4L to User(4L, "prod-email-4@gmail.com", "Name 4", 50)
  )

}
</pre>



<p>Well, in our application we will be dealing with dummy data.</p>



<p>However, in real life instead of two maps implementations, we would rather have a few implementations of UserRepository, for example:</p>



<ul class="wp-block-list">
<li>one, in-memory, which could be used for local development and testing,</li>



<li>the second, &#8220;real&#8221;, one, responsible for database connection.</li>
</ul>



<p>Either way, the purpose of this tutorial is to learn a bit more about Kotlin with bean definition DSL, and this setup will be useful in our considerations.</p>



<h2 class="wp-block-heading" id="h-5-configure-routing">5. Configure Routing</h2>



<p>In order to expose REST endpoints with Kotlin route DSL, we need to add two things:</p>



<ul class="wp-block-list">
<li><code class="EnlighterJSRAW" data-enlighter-language="raw">UserHandler</code>&#8211; which normally would be a UserController when working with annotations,</li>



<li>custom <code class="EnlighterJSRAW" data-enlighter-language="raw">RouteFunctionDSL</code>&#8211; responsible for URL mappings.</li>
</ul>



<h3 class="wp-block-heading" id="h-5-1-add-userhandler">5.1 Add UserHandler</h3>



<p>Firstly, let&#8217;s add a <code class="EnlighterJSRAW" data-enlighter-language="raw">handler</code> package and implement the <code class="EnlighterJSRAW" data-enlighter-language="raw">UserHandler</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class UserHandler(
  private val userRepository: UserRepository
) {

  fun createUser(
    request: ServerRequest
  ): ServerResponse {
    val userRequest = request.body(UserDTO::class.java)

    val createdUserResponse = userRepository.create(
      user = userRequest.toModel()
    )
      .toDTO()

    return ServerResponse.ok()
      .body(createdUserResponse)
  }

  fun findAllUsers(
    request: ServerRequest
  ): ServerResponse {
    val usersResponses = userRepository.findAll()
      .map(User::toDTO)

    return ServerResponse.ok()
      .body(usersResponses)
  }

  fun findUserById(
    request: ServerRequest
  ): ServerResponse {
    val id = request.pathVariable("id")
      .toLongOrNull()
      ?: return badRequestResponse("Invalid id")

    val userResponse = userRepository.findById(id)
      ?.toDTO()

    return userResponse
      ?.let { response ->
        ServerResponse.ok()
          .body(response)
      }
      ?: notFoundResponse(id)
  }

  fun updateUserById(
    request: ServerRequest
  ): ServerResponse {
    val id = request.pathVariable("id")
      .toLongOrNull()
      ?: return badRequestResponse("Invalid id")

    val userRequest = request.body(UserDTO::class.java)

    val updatedUser = userRepository.updateById(
      id = id,
      user = userRequest.toModel()
    )

    return updatedUser
      ?.let { response ->
        ServerResponse.ok()
          .body(response)
      }
      ?: notFoundResponse(id)
  }

  fun deleteUserById(
    request: ServerRequest
  ): ServerResponse {
    val id = request.pathVariable("id")
      .toLongOrNull()
      ?: return badRequestResponse("Invalid id")

    userRepository.deleteById(id)

    return ServerResponse.noContent()
      .build()
  }

  private fun badRequestResponse(reason: String): ServerResponse =
    ServerResponse.badRequest()
      .body(
        ErrorResponse(reason)
      )

  private fun notFoundResponse(id: Long): ServerResponse =
    ServerResponse.badRequest()
      .body(
        ErrorResponse("User with id: $id was not found.")
      )

}

private fun UserDTO.toModel(): User =
  User(
    email = this.email,
    name = this.name,
    age = this.age
  )

private fun User.toDTO(): UserDTO =
  UserDTO(
    email = this.email,
    name = this.name,
    age = this.age
  )
</pre>



<p>As we can see, the handler has only one parameter of the <code class="EnlighterJSRAW" data-enlighter-language="raw">UserRepository</code>&nbsp; type. Thanks do that, <strong>we are not coupled with any implementation</strong>. Moreover, we gained flexibility and we can provide the implementation even on the fly when configuring beans.</p>



<p>Another interesting thing is the usage of <em>ServerRequests</em> and <em>ServerResponses</em>. When working with Spring Boot and Kotlin routes DSL, that&#8217;s how we deal with incoming requests and responses. And although for more details I redirect you to my previous article about this topic, I wanted to mention this because of one thing. As you can see- instead of throwing exceptions, which then Spring would translate to HTTP responses, we have the possibility to return our custom payload (and status codes, as well).</p>



<h3 class="wp-block-heading" id="h-5-2-implement-routing">5.2 Implement Routing</h3>



<p>Nevertheless, handler implementation is not sufficient. We have to instruct Spring about how HTTP requests should be handled.</p>



<p>To do so, let&#8217;s add the <code class="EnlighterJSRAW" data-enlighter-language="raw">Routes.kt</code> file inside the <code class="EnlighterJSRAW" data-enlighter-language="raw">config</code> package:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun appRouter(userHandler: UserHandler) = router {
  "/api".nest {
    "/users".nest {
      POST(userHandler::createUser)
      GET(userHandler::findAllUsers)
      GET("/{id}", userHandler::findUserById)
      PUT("/{id}", userHandler::updateUserById)
      DELETE("/{id}", userHandler::deleteUserById)
    }
  }
}
</pre>



<p>The <code class="EnlighterJSRAW" data-enlighter-language="raw">appRouter</code> function has one parameter of the <code class="EnlighterJSRAW" data-enlighter-language="raw">UserHandler</code> type, which we then use to provide references to functions inside it.</p>



<p>It&#8217;s worth mentioning that function references in Kotlin are an interesting syntactic sugar. However, if you prefer not to use them, you can always simply make use of brackets:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">POST { request -> userHandler.createUser(request) }
</pre>



<h2 class="wp-block-heading" id="h-6-kotlin-bean-definition-dsl">6. Kotlin Bean Definition DSL</h2>



<p>With all of that being done, we can finally learn <strong>how the Kotlin bean definition DSL work with Spring Boot 3</strong>.</p>



<h2 class="wp-block-heading" id="h-6-1-implement-beansconfig">6.1 Implement BeansConfig</h2>



<p>Firstly, let&#8217;s add a new file called <code class="EnlighterJSRAW" data-enlighter-language="raw">BeansConfig.kt</code> inside the <code class="EnlighterJSRAW" data-enlighter-language="raw">config</code> package:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val beans = beans {
  // beans definitions
}
</pre>



<p>The <code class="EnlighterJSRAW" data-enlighter-language="raw">beans {}</code> is nothing else than a function, which leverages the concept of <a href="https://kotlinlang.org/docs/type-safe-builders.html" target="_blank" rel="noopener">type-safe builders</a>.</p>



<p>Secondly, let&#8217;s add the <em>BeansConfig</em> class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class BeansConfig : ApplicationContextInitializer&lt;GenericApplicationContext> {

  override fun initialize(context: GenericApplicationContext) =
    beans.initialize(context)

}
</pre>



<p>In order to register the beans in our application context, we have to invoke the <code class="EnlighterJSRAW" data-enlighter-language="raw">initialize()</code>.</p>



<p>Lastly, we need to make Spring aware of our initializer in <code class="EnlighterJSRAW" data-enlighter-language="raw">application.yaml</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">context:
  initializer:
    classes: com.codersee.kotlindsl.config.BeansConfig
</pre>



<h2 class="wp-block-heading" id="h-6-2-autowiring-by-type">6.2 Autowiring By Type</h2>



<p>As the next step, let&#8217;s see how we can define <em>UserHandler</em> and <em>router</em> beans and instruct Spring to<strong> autowire by type</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">bean&lt;UserHandler>()
bean(::appRouter)
</pre>



<p>As we can see, we don&#8217;t even have to specify the types of particular parameters and Spring will take care of that automatically.</p>



<p>Moreover, with<strong> callable reference</strong>, we can define a bean with the <code class="EnlighterJSRAW" data-enlighter-language="raw">appRouter</code> top-level function.</p>



<h2 class="wp-block-heading" id="h-6-3-explicitly-specify-bean-type">6.3 Explicitly Specify Bean Type</h2>



<p>Of course, if we would like to specify the type of the bean manually or wire by name, then we can do it, as well:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">bean("myHandlerBean") {
  UserHandler(ref())
}
bean {
  appRouter(
    ref("myHandlerBean")
  )
}
</pre>



<p>And this time we simply make use of the ref function, which is responsible for getting a reference to beans by type (line 2), or by type and name (line 6).</p>



<h2 class="wp-block-heading" id="h-6-4-conditional-beans-based-on-profile">6.4 Conditional Beans Based On Profile</h2>



<p>Following, let&#8217;s see how we can differentiate the data source based on the profile property:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">profile("dev") {
  bean {
    UserCrudRepository(devDataSource)
  }
}

profile("prod") {
  bean {
    UserCrudRepository(prodDataSource)
  }
}
</pre>



<p>With this approach, beans defined inside the <code class="EnlighterJSRAW" data-enlighter-language="raw">profile()</code> won&#8217;t be created unless the specified profile is active.</p>



<p>Of course, we can activate profiles, for example inside the <code class="EnlighterJSRAW" data-enlighter-language="raw">application.yaml</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">spring:
  profiles:
    active: dev
</pre>



<h2 class="wp-block-heading" id="h-6-5-define-additional-beans-properties">6.5 Define Additional Beans Properties</h2>



<p>Following, let&#8217;s see how we can customize bean definitions:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">bean(
  name = "userHandler",
  scope = BeanDefinitionDsl.Scope.SINGLETON,
  isLazyInit = true,
  isPrimary = true,
  isAutowireCandidate = true,
  initMethodName = "",
  destroyMethodName = "",
  description = "description",
  role = BeanDefinitionDsl.Role.APPLICATION
)
</pre>



<p>As we can see, all parameters of the <code class="EnlighterJSRAW" data-enlighter-language="raw">bean</code> function have default values assigned.</p>



<p>Nevertheless, if we would like to set any values explicitly, then we can do it with ease.</p>



<h2 class="wp-block-heading" id="h-6-6-reading-environment-variables">6.6 Reading Environment Variables</h2>



<p>As the last thing, let&#8217;s see how we can read environment variables:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val someVariable = env.systemEnvironment["SOME_VARIABLE"]
</pre>



<p>The systemEnvironment is nothing else than a <code class="EnlighterJSRAW" data-enlighter-language="raw">Map&lt;String, Object&gt;</code> instance, which is a result of <code class="EnlighterJSRAW" data-enlighter-language="raw">System.getenv()</code> invocation.</p>



<h2 class="wp-block-heading" id="h-7-spring-boot-3-kotlin-dsl-summary">7. Spring Boot 3 Kotlin DSL Summary</h2>



<p>And that&#8217;s all for this tutorial about how to implement a REST API without annotations using Spring Boot 3, Kotlin bean definition, and router DSL.</p>



<p>I&#8217;m happy to<strong> hear your feedback</strong>&#8211; trust me, such a simple comment can motivate a lot and help me to work on my weaknesses 🙂</p>



<p>Of course, if you would like to see the source code for this article, then you can find it in this <a href="https://github.com/codersee-blog/kotlin-spring-boot-3-kotlin-bean-definition-router-dsl" target="_blank" rel="noopener">GitHub repository</a>.</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/">Spring Boot 3 With Kotlin DSL. REST API Without Annotations</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/spring-boot-3-kotlin-dsl-rest-api-without-annotations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Spring Boot 3 Kotlin Router DSL</title>
		<link>https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/</link>
					<comments>https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Tue, 21 Mar 2023 07:00:45 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Coroutines]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Spring WebFlux]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=6004428</guid>

					<description><![CDATA[<p>In this practical tutorial, I will show you how to expose web endpoints using Spring Boot 3 and Kotlin router DSL (functional style).</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/">Spring Boot 3 Kotlin Router DSL</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="h-1-introduction">1. Introduction</h2>



<p>Hello friend! 🙂 Welcome to my next practical tutorial, in which I will show you how to expose web endpoints using <strong>Spring Boot 3</strong> and <strong>Kotlin router DSL</strong> (aka- functional style).</p>



<p>Basically, the Kotlin router DSL comes in 3 variants:</p>



<ul class="wp-block-list">
<li><em>WebMvc.fn DSL</em> using <code class="EnlighterJSRAW" data-enlighter-language="raw">router { }</code> &#8211; which we can use when working with a &#8220;standard&#8221; MVC Spring Project,</li>



<li><em>WebFlux.fn Reactive DSL</em> with <code class="EnlighterJSRAW" data-enlighter-language="raw">router { }</code> &#8211; used when working with a reactive-stack framework- Spring Webflux,</li>



<li><em>WebFlux.fn Coroutines DSL</em> with <code class="EnlighterJSRAW" data-enlighter-language="raw">coRouter { }</code> &#8211; as the name suggests- the one we will use when working with coroutines.</li>
</ul>



<p>And although in this article, we will make use of the coroutine-based REST API implemented in &#8220;<a href="https://blog.codersee.com/reactive-rest-api-with-spring-kotlin-and-coroutines/">Reactive REST API With Spring, Kotlin, and Coroutines</a>&#8220;, the knowledge you&#8217;re gonna gain today is universal and will work regardless of the implementation.</p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<p></p></div>


<a href="https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2FfS23KwIBqfc%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<p>If you find this content useful,<strong> please leave a subscription&nbsp;</strong> 😉</p>



<h2 class="wp-block-heading" id="h-2-project-configuration">2. Project Configuration</h2>



<p>As I mentioned in the introduction, we will reuse the already implemented API and rewrite the @RestController approach using the Spring Boot Kotlin DSL router.</p>


<p>[elementor-template id=&#8221;9007393&#8243;]</p>



<p>So, as the first step, let&#8217;s clone this <a href="https://github.com/codersee-blog/spring-boot-3-kotlin-coroutines" target="_blank" rel="noopener">GitHub repository.</a></p>



<p>When we navigate to the <code class="EnlighterJSRAW" data-enlighter-language="raw">controller</code> package, we will see that the project contains 3 controllers:</p>



<ul class="wp-block-list">
<li>UserController</li>



<li>SearchController</li>



<li>CompanyController</li>
</ul>



<p>And for the purpose of this tutorial, we will focus on the API responsible for users management:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@RestController
@RequestMapping("/api/users")
class UserController(
    private val userService: UserService
) {

  @PostMapping
  suspend fun createUser(@RequestBody userRequest: UserRequest): UserResponse =
    userService.saveUser(
        user = userRequest.toModel()
    )
      ?.toResponse()
      ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during user creation.")

  @GetMapping
  suspend fun findUsers(
    @RequestParam("name", required = false) name: String?
  ): Flow {
    val users = name?.let { userService.findAllUsersByNameLike(name) }
      ?: userService.findAllUsers()

    return users.map(User::toResponse)
  }

  @GetMapping("/{id}")
  suspend fun findUserById(
    @PathVariable id: Long
  ): UserResponse =
    userService.findUserById(id)
      ?.let(User::toResponse)
      ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "User with id $id was not found.")

  @DeleteMapping("/{id}")
  suspend fun deleteUserById(
    @PathVariable id: Long
  ) {
    userService.deleteUserById(id)
  }

  @PutMapping("/{id}")
  suspend fun updateUser(
    @PathVariable id: Long,
    @RequestBody userRequest: UserRequest
  ): UserResponse =
    userService.updateUser(
      id = id,
      requestedUser = userRequest.toModel()
    )
      .toResponse()
}

private fun UserRequest.toModel(): User =
  User(
    email = this.email,
    name = this.name,
    age = this.age,
    companyId = this.companyId
  )

fun User.toResponse(): UserResponse =
  UserResponse(
    id = this.id!!,
    email = this.email,
    name = this.name,
    age = this.age
  )
</pre>



<h2 class="wp-block-heading" id="h-3-convert-usercontroller-to-handler">3. Convert UserController To Handler</h2>



<p>Before we will start working with Spring Boot Kotlin DSL, let&#8217;s make a few adjustments.</p>



<p>Firstly, let&#8217;s add a new package called <code class="EnlighterJSRAW" data-enlighter-language="raw">handler</code>.</p>



<p>Then, let&#8217;s remove all annotations inside the class and mark the <em>UserHandler</em> with <em>@Component</em>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Component
class UserHandler(
    private val userService: UserService
) {

  suspend fun createUser(userRequest: UserRequest): UserResponse =
    userService.saveUser(
      user = userRequest.toModel()
    )
      ?.toResponse()
      ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during user creation.")

  suspend fun findUsers(
    name: String?
  ): Flow {
    val users = name?.let { userService.findAllUsersByNameLike(name) }
      ?: userService.findAllUsers()

    return users.map(User::toResponse)
  }

  suspend fun findUserById(
    id: Long
  ): UserResponse =
    userService.findUserById(id)
      ?.let(User::toResponse)
      ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "User with id $id was not found.")

  suspend fun deleteUserById(
    id: Long
  ) {
    userService.deleteUserById(id)
  }

  suspend fun updateUser(
    id: Long,
    userRequest: UserRequest
  ): UserResponse =
    userService.updateUser(
      id = id,
      requestedUser = userRequest.toModel()
    )
      .toResponse()
}
</pre>



<h2 class="wp-block-heading" id="h-4-implement-coroutines-router-with-kotlin-dsl">4. Implement Coroutines router with Kotlin DSL</h2>



<p>With all of that being done, let&#8217;s create a new class <em>Config</em> inside the <code class="EnlighterJSRAW" data-enlighter-language="raw">config</code> package:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Configuration
class Config {

  @Bean
  fun router(
    userHandler: UserHandler
  ) = coRouter {
    accept(MediaType.APPLICATION_JSON).nest {

      "/api/users".nest {
        POST("", userHandler::createUser)
        GET("", userHandler::findUsers)
        GET("/{id}", userHandler::findUserById)
        DELETE("/{id}", userHandler::deleteUserById)
        PUT("/{id}", userHandler::updateUser)
      }

    }
  }

}
</pre>



<p>As I mentioned in the beginning when working with coroutines, we will create a <strong>RouterFunction using coRouter{ }</strong>. Nevertheless, when working with MVC or Spring WebFlux, then the choice would be the <strong>router {}</strong>.</p>



<p>As we can see, the <strong>nest {}</strong> function allows us to structure routing in a neat and readable manner.</p>



<p>Let&#8217;s take a look at the following example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">"/api/v1".nest {
  "/whatever".nest {
    "/sub-whatever".nest {
      // whatever
    }
  }
}

"/api/v2".nest {
  // another handlers
}
</pre>



<p>This way, we can manage versioning in one place.</p>



<p>Nevertheless, let&#8217;s get back to our user handlers:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">POST("", userHandler::createUser)
GET("", userHandler::findUsers)
GET("/{id}", userHandler::findUserById)
DELETE("/{id}", userHandler::deleteUserById)
PUT("/{id}", userHandler::updateUser)
</pre>



<p>At this point, our application won&#8217;t compile, because of one, important thing- <strong>handler functions must take only one parameter of the ServerRequest type</strong>.</p>



<p>So in order to fix the app, we will have to update our Spring controller for functional routing.</p>



<h2 class="wp-block-heading" id="h-5-update-post-endpoint">5. Update POST Endpoint</h2>



<p>So as the first step, let&#8217;s make the necessary adjustments to the <code class="EnlighterJSRAW" data-enlighter-language="raw">createUser</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun createUser(request: ServerRequest): ServerResponse {
  val userRequest = request.awaitBody(UserRequest::class)

  val savedUserResponse = userService.saveUser(
    user = userRequest.toModel()
  )
    ?.toResponse()
    ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during user creation.")

  return ServerResponse.ok()
    .bodyValueAndAwait(savedUserResponse)
}

// Before:


@PostMapping
suspend fun createUser(@RequestBody userRequest: UserRequest): UserResponse =
  userService.saveUser(
    user = userRequest.toModel()
  )
    ?.toResponse()
    ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during user creation.")
</pre>



<p>As we can see, instead of binding the JSON payload to the UserRequest instance with an annotation, we have to do it manually with <code class="EnlighterJSRAW" data-enlighter-language="raw">.awaitBody()</code>.</p>



<p>Whatsoever, the return type changes to <strong>ServerResponse</strong>, which we have to create manually.</p>



<p>And although the final choice is up to you, instead of throwing the ResponseStatusException, we can compose the HTTP error response on our own:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun createUser(request: ServerRequest): ServerResponse {
  val userRequest = request.awaitBody(UserRequest::class)

  return userService.saveUser(
    user = userRequest.toModel()
  )
    ?.toResponse()
    ?.let { response ->
      ServerResponse.ok()
        .bodyValueAndAwait(response)
    }
    ?: ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
      .buildAndAwait()
}
</pre>



<h2 class="wp-block-heading" id="h-6-migrate-get-all-users">6. Migrate GET All Users</h2>



<p>Nextly, let&#8217;s migrate the <code class="EnlighterJSRAW" data-enlighter-language="raw">findUsers()</code> function and learn how to read query parameters:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun findUsers(
  request: ServerRequest
): ServerResponse {
  val users = request.queryParamOrNull("name")
    ?.let { name -> userService.findAllUsersByNameLike(name) }
    ?: userService.findAllUsers()

  val usersResponse = users.map(User::toResponse)

  return ServerResponse.ok()
    .bodyAndAwait(usersResponse)
}

// Before:

@GetMapping
suspend fun findUsers(
  @RequestParam("name", required = false) name: String?
): Flow {
  val users = name?.let { userService.findAllUsersByNameLike(name) }
    ?: userService.findAllUsers()

  return users.map(User::toResponse)
} 
</pre>



<p>This time, in order to read a particular query parameter, we can use the <code class="EnlighterJSRAW" data-enlighter-language="raw">queryParamOrNull(String)</code>. However, the ServerRequest interface comes with two, additional functions, which we could use here:</p>



<ul class="wp-block-list">
<li><code class="EnlighterJSRAW" data-enlighter-language="raw">queryParam(String)</code>, which returns the <em>Optional</em>,</li>



<li>and <code class="EnlighterJSRAW" data-enlighter-language="raw">queryParams()</code> returning an instance of <em>MultiValueMap&lt;String, String&gt;</em> with all parameters.</li>
</ul>



<p>And just, like previously we compose a ServerResponse instance, but this time using the <code class="EnlighterJSRAW" data-enlighter-language="raw">bodyAndAwait()</code>, which takes a <em>Flow</em> as an argument.</p>



<h2 class="wp-block-heading" id="h-7-get-user-by-id">7. GET User By Id</h2>



<p>As the next example in our Spring Boot Kotlin DSL project, let&#8217;s rewrite the <code class="EnlighterJSRAW" data-enlighter-language="raw">findUserById()</code> and see how we can read a <strong>path variable</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun findUserById(
  request: ServerRequest
): ServerResponse {
  val id = request.pathVariable("id").toLong()

  val userResponse = userService.findUserById(id)
    ?.let(User::toResponse)
    ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "User with id $id was not found.")

  return ServerResponse.ok()
    .bodyValueAndAwait(userResponse)
}

// Before:

@GetMapping("/{id}")
suspend fun findUserById(
  @PathVariable id: Long
): UserResponse =
  userService.findUserById(id)
    ?.let(User::toResponse)
    ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "User with id $id was not found.")
</pre>



<p>As we can see, in order to read the path variable, we have to use the <code class="EnlighterJSRAW" data-enlighter-language="raw">pathVariable(String)</code> (or alternatively, <code class="EnlighterJSRAW" data-enlighter-language="raw">pathVariables()</code>). Unfortunately, this function cannot be parametrized and we have to take care of Long conversions manually.</p>



<h2 class="wp-block-heading" id="h-8-delete-endpoint">8. DELETE Endpoint</h2>



<p>Following, let&#8217;s update the <code class="EnlighterJSRAW" data-enlighter-language="raw">deleteUserById()</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun deleteUserById(
  request: ServerRequest
): ServerResponse {
  val id = request.pathVariable("id").toLong()

  userService.deleteUserById(id)

  return ServerResponse.noContent()
    .buildAndAwait()
}

// Before: 

@DeleteMapping("/{id}")
suspend fun deleteUserById(
  @PathVariable id: Long
) {
  userService.deleteUserById(id)
}
</pre>



<p>And this time, instead of returning anything in the response payload, we make use of the <code class="EnlighterJSRAW" data-enlighter-language="raw">noContent()</code> along with <code class="EnlighterJSRAW" data-enlighter-language="raw">buildAndAwait()</code> to return a bodiless entity.</p>



<h2 class="wp-block-heading" id="h-9-user-update-with-put">9. User Update With PUT</h2>



<p>Lastly, let&#8217;s make the necessary adjustments to the <code class="EnlighterJSRAW" data-enlighter-language="raw">updateUser()</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">suspend fun updateUser(
  request: ServerRequest
): ServerResponse {
  val id = request.pathVariable("id").toLong()
  val userRequest = request.awaitBody(UserRequest::class)

  val userResponse = userService.updateUser(
    id = id,
    requestedUser = userRequest.toModel()
  ).toResponse()

  return ServerResponse.ok()
    .bodyValueAndAwait(userResponse)
}

// Before:

@PutMapping("/{id}")
suspend fun updateUser(
  @PathVariable id: Long,
  @RequestBody userRequest: UserRequest
): UserResponse =
  userService.updateUser(
    id = id,
    requestedUser = userRequest.toModel()
  )
    .toResponse()
</pre>



<p>As we can see, nothing new shows up in this example.</p>



<h2 class="wp-block-heading" id="h-10-testing-and-homework">10. Testing And Homework</h2>



<p>With all of that being done, we can finally run our application and verify whether handlers are working, as expected.</p>



<p>Your homework will be to:</p>



<ol class="wp-block-list">
<li>Test whether the routing works fine after we converter our Spring controller to the functional style.</li>



<li>Refactor the <em>CompanyController</em> and <em>SearchController</em> classes in a similar manner.</li>
</ol>



<p>As a bonus, right here you can find a <a href="https://drive.google.com/file/d/1xq3njZk2oGwHQGZkTFf05VwHr1pdsJJE/view?usp=sharing" target="_blank" rel="noopener">Postman collection</a>, which you can use for testing.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Note: If you would like to see the solution, then check out the <a href="https://github.com/codersee-blog/spring-boot-3-kotlin-coroutines/tree/router-dsl" target="_blank" rel="noopener">router-dsl branch</a> in my GitHub repo.</p>
</blockquote>



<h2 class="wp-block-heading" id="h-11-spring-boot-3-with-kotlin-dsl-summary">11. Spring Boot 3 With Kotlin DSL Summary</h2>



<p>And that&#8217;s all for this tutorial, in which we have learned how to expose web endpoints using <strong>Spring Boot 3</strong> and <strong>Kotlin router DSL</strong> (aka- functional style).</p>



<p>Hope you enjoyed this content, and if so, then please <strong>reach out in the comments section</strong>.</p>



<p>Have a great week!</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/">Spring Boot 3 Kotlin Router DSL</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/spring-boot-3-kotlin-router-dsl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Postgres jsonb to Jackson JsonNode With Spring Data JPA</title>
		<link>https://blog.codersee.com/postgres-jsonb-to-jackson-jsonnode-jpa/</link>
					<comments>https://blog.codersee.com/postgres-jsonb-to-jackson-jsonnode-jpa/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Mon, 12 Dec 2022 06:00:28 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=5504034</guid>

					<description><![CDATA[<p>This time, I would like to show you how to solve a pretty common issue when mapping Postgres jsonb to Jackson JsonNode in Spring Data JPA.</p>
<p>The post <a href="https://blog.codersee.com/postgres-jsonb-to-jackson-jsonnode-jpa/">Postgres jsonb to Jackson JsonNode With Spring Data JPA</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="h-1-introduction">1. Introduction</h2>



<p>Hello friend! In this post, I would like to show you how to solve a pretty common issue when mapping <strong>Postgres jsonb to Jackson JsonNode in Spring Data JPA</strong>.</p>



<p>If you are not a regular reader of my blog, then I&#8217;m pretty sure that you wanted to map a PostgreSQL jsonb field in your Spring Boot with Spring Data JPA application to Jsonnode and you saw something like that:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#8216;entityManagerFactory&#8217; defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Could not determine recommended JdbcType for `com.fasterxml.jackson.databind.JsonNode`</p>
</blockquote>



<p>If that&#8217;s the case, then you&#8217;ve come to the right place. 😁</p>



<h2 class="wp-block-heading" id="h-2-quick-setup">2. Quick Setup</h2>



<p>Before we learn how to solve the issue with Postgres jsonb mapping to JsonNode, let&#8217;s take a minute to reproduce the error.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Note:</strong> If you would like to cut straight to the point and see how to implement a mapper, then you can skip this part and navigate directly to <strong>chapter 3.</strong></p>
</blockquote>



<h3 class="wp-block-heading" id="h-2-1-postgresql-table-with-jsonb-column">2.1. PostgreSQL Table With jsonb column</h3>



<p>Firstly, we need to have a PostgreSQL table with jsonb column:</p>



<figure class="wp-block-image aligncenter wp-image-5504040 size-full"><img loading="lazy" decoding="async" width="407" height="306" src="http://blog.codersee.com/wp-content/uploads/2022/12/postgres_jsonb_to_jsonnode_db_table_diagram.png" alt="Image shows a diagram of PostgreSQL table called some_table with columns definitions (including a jsonb column), which will be used to demonstrate how to map Postgres jsonb to JsonNode." class="wp-image-5504040" srcset="https://blog.codersee.com/wp-content/uploads/2022/12/postgres_jsonb_to_jsonnode_db_table_diagram.png 407w, https://blog.codersee.com/wp-content/uploads/2022/12/postgres_jsonb_to_jsonnode_db_table_diagram-300x226.png 300w" sizes="auto, (max-width: 407px) 100vw, 407px" /><figcaption class="wp-element-caption">Diagram generated with <a href="https://dbdiagram.io/" target="_blank" rel="noopener">https://dbdiagram.io/</a></figcaption></figure>



<p>As we can see, it&#8217;s just a dummy table called&nbsp;<em>some_table</em>, which contains 4 example columns.</p>



<p>And to be on the same it page, I created it and populated with the following SQL script:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">create table some_table(
  id serial not null primary key,
  string_column text not null,
  integer_column smallint not null,
  jsonb_column jsonb not null
);

insert into 
  some_table(string_column, integer_column, jsonb_column)
values
  ('value-1', 1, '{ "some_field": "some_value"}'),
  ('value-2', 2, '{ "another_field": "some_value_2"}');</pre>


<p>[elementor-template id=&#8221;9007393&#8243;]</p>



<h3 class="wp-block-heading" id="h-2-2-spring-boot-and-dependencies">2.2. Spring Boot and Dependencies</h3>



<p>Nextly, let&#8217;s create an example Spring Boot project. As always, we can do that with the <a href="https://start.spring.io/" target="_blank" rel="noopener">Spring Initializr page</a>.</p>



<p>Please configure it according to your needs, but do not forget to add the <strong>PostgreSQL Driver</strong> and <strong>Spring Data JPA</strong> dependencies. Additionally, we will need to enrich the project with <strong>Jackson Databind</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.1")
runtimeOnly("org.postgresql:postgresql")</pre>



<h3 class="wp-block-heading" id="h-2-3-application-yaml">2.3. application.yaml</h3>



<p>With that being done, let&#8217;s specify the connection details:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">spring:
  datasource:
    url: "jdbc:postgresql://localhost:5432/postgres"
      username: postgres
      password: postgres</pre>



<h3 class="wp-block-heading" id="h-2-4-implement-entity">2.4. Implement Entity</h3>



<p>As the next step, let&#8217;s open up the project and add the <em>SomeEntity</em> data class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Entity
@Table(name = "some_table")
data class SomeEntity(
  @Id
  val id: Long? = null,
  val stringColumn: String,
  val integerColumn: Int,
  val jsonbColumn: JsonNode
)</pre>



<p>As we can see, we use the <strong>@Entity</strong> annotation to mark the class as the entity and <strong>@Table</strong> to explicitly provide Spring with the table name. Moreover, the necessary <strong>@Id</strong> annotation specifies the primary key of our entity and the <em>jsonbColumn</em> will contain the value of the Postgres jsonb column mapped to JsonNode.</p>



<h3 class="wp-block-heading" id="h-2-5-create-jpa-repository">2.5. Create JPA repository</h3>



<p>With that being done, let&#8217;s add the <em>SomeEntityRepository</em> interface:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">interface SomeEntityRepository : CrudRepository&lt;SomeEntity, Long></pre>



<p>This time, the only thing we have to do is to extend the CrudRepository interface, which comes with a basic CRUD operations.</p>



<h3 class="wp-block-heading" id="h-2-6-create-jpa-repository">2.6. Create JPA repository</h3>



<p>Lastly, let&#8217;s add the logic, which will be run when the application starts:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Component
class MyRunner(
  private val repository: SomeEntityRepository
) : CommandLineRunner {

  override fun run(vararg args: String?) {
    repository.findAll()
      .forEach(::println)
  }
}</pre>



<p>As we can see, a repository instance will be injected into this simple component. Moreover, we implement the <em>run</em> method of <em>CommandLineRunner</em>, which instructs Spring Boot to run after the app starts (if you would like to learn more about it, then check out my article about the <a href="https://blog.codersee.com/spring-boot-console-app-with-kotlin/" target="_blank" rel="noopener">Spring Boot console app</a>).</p>



<p>Finally, we want to print out all entities, nevertheless, such a stack trace appears to our eyes:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Could not determine recommended JdbcType for `com.fasterxml.jackson.databind.JsonNode`
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[spring-context-6.0.2.jar:6.0.2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[spring-context-6.0.2.jar:6.0.2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.2.jar:6.0.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.0.jar:3.0.0]
at com.codersee.jsonbtojsonnode.JsonbToJsonNodeApplicationKt.main(JsonbToJsonNodeApplication.kt:14) ~[main/:na]
Caused by: org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException: Could not determine recommended JdbcType for `com.fasterxml.jackson.databind.JsonNode`
at org.hibernate.type.descriptor.java.spi.UnknownBasicJavaType.getRecommendedJdbcType(UnknownBasicJavaType.java:37) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.boot.model.process.internal.InferredBasicValueResolver.from(InferredBasicValueResolver.java:222) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.mapping.BasicValue.buildResolution(BasicValue.java:507) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.mapping.BasicValue.resolve(BasicValue.java:315) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.mapping.BasicValue.resolve(BasicValue.java:305) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.lambda$processValueResolvers$4(InFlightMetadataCollectorImpl.java:1767) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at java.base/java.util.ArrayList.removeIf(ArrayList.java:1682) ~[na:na]
at java.base/java.util.ArrayList.removeIf(ArrayList.java:1660) ~[na:na]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processValueResolvers(InFlightMetadataCollectorImpl.java:1766) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1752) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:300) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1350) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1421) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.0.2.jar:6.0.2]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.2.jar:6.0.2]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.2.jar:6.0.2]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.2.jar:6.0.2]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.2.jar:6.0.2]
... 15 common frames omitted</pre>



<h2 class="wp-block-heading" id="h-3-map-postgres-jsonb-to-jsonnode">3. Map Postgres jsonb to JsonNode</h2>



<p>So at this point, we can see that Spring &#8220;<em>Could not determine recommended JdbcType for `com.fasterxml.jackson.databind.JsonNode`</em>&#8220;.</p>



<figure class="wp-block-image aligncenter"><a href="https://codersee.com/newsletter/"><img loading="lazy" decoding="async" width="1024" height="576" src="http://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter-1024x576.png" alt="Image shows two ebooks people can get for free after joining newsletter" class="wp-image-3002956" srcset="https://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter-1024x576.png 1024w, https://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter-300x169.png 300w, https://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter-768x432.png 768w, https://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter.png 1440w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>One of the possible solutions is to implement a custom <strong>AttributeConverter</strong> and mark it with <strong>@Converter</strong> annotation:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Converter(autoApply = true)
class MyConverter : AttributeConverter&lt;JsonNode, String> {

  private val mapper = ObjectMapper()

  override fun convertToDatabaseColumn(node: JsonNode): String {
    return node.asText()
  }

  override fun convertToEntityAttribute(data: String): JsonNode {
    return mapper.readTree(data)
  }
}</pre>



<p>As we can see, the <em>AttributeConverter</em> interface comes with two methods: <em>convertToDatabaseColumn</em> and <em>convertToEntityAttribute</em>, which will be invoked when mapping between the entity and the database.</p>



<p>Additionally, we have set the <strong>autoApply</strong> to <em>true</em> (the default is <em>false</em>) to inform the persistence provider that it should use this converter to map between Postgres jsonb and JsonNode. Without it, we would have to explicitly annotate the desired field:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Convert(converter = MyConverter::class)
val jsonbColumn: JsonNode</pre>



<p>Of course, the choice is up to you and may vary depending on your project, but most of the time you would like to choose the first solution.</p>



<p>Finally, if we run the application, we should see that everything is working, as expected and logs contain the following lines:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SomeEntity(id=1, stringColumn=value-1, integerColumn=1, jsonbColumn={"some_field":"some_value"})
SomeEntity(id=2, stringColumn=value-2, integerColumn=2, jsonbColumn={"another_field":"some_value_2"})</pre>



<h2 class="wp-block-heading" id="h-4-summary">4. Summary</h2>



<p>And that&#8217;s all for this short, hands-on article on how to map Postgres jsonb to JsonNode. As always, you can find the source code in <a href="https://github.com/codersee-blog/spring-boot-kotlin-jsonb-to-jsonnode" target="_blank" rel="noopener">this GitHub repository</a>.</p>



<p>As always, happy to hear your thoughts on how would you solve this issue.</p>
<p>The post <a href="https://blog.codersee.com/postgres-jsonb-to-jackson-jsonnode-jpa/">Postgres jsonb to Jackson JsonNode With Spring Data JPA</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/postgres-jsonb-to-jackson-jsonnode-jpa/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Add Flyway To An Existing Spring Boot Project</title>
		<link>https://blog.codersee.com/add-flyway-to-existing-spring-boot-project/</link>
					<comments>https://blog.codersee.com/add-flyway-to-existing-spring-boot-project/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Thu, 08 Dec 2022 18:16:48 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Flyway]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=5504025</guid>

					<description><![CDATA[<p>In this, the third article about Flyway on my blog, we will see how to add Flyway to an existing Spring Boot project. </p>
<p>The post <a href="https://blog.codersee.com/add-flyway-to-existing-spring-boot-project/">Add Flyway To An Existing Spring Boot Project</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2 class="article-heading-introduction">1. Introduction</h2>
<p>In this article, which is a continuation of a <a href="https://flywaydb.org/" target="_blank" rel="noopener">Flyway</a> series we will learn how to <strong>add Flyway to an existing Spring Boot Project</strong>.</p>
<p>If you are not sure what this tool can bring to your project, then check out my first post on <a href="https://blog.codersee.com/flyway-migrations-with-spring-boot/">Flyway with Spring Boot</a>, where you can find all the necessary information.</p>
<h2 class="article-heading-introduction">2. Project Setup</h2>
<p>This time, we won&#8217;t spend time preparing a sample project (well, if you are here then there&#8217;s a 95% chance that you already have some project).</p>
<p>Nevertheless, if you don&#8217;t have one, but still would like to learn how to add a flyway to an existing Spring Boot project, then no worries. You can find an example project in<a href="https://github.com/codersee-blog/spring-boot-add-flyway-to-existing-project" target="_blank" rel="noopener"> this GitHub repository</a>.</p>
<blockquote><p><strong>Note: </strong>If you use the project from the linked repository, then please run the script.sql file (can be found in /resources/sql directory) against your database, before you start the application.</p></blockquote>
<h2 class="article-heading-introduction">3. Strategy For Adding Flyway To Existing Spring Boot Project</h2>
<p>Before we start anything, I just wanted to emphasize that <strong>we should always create a backup, before performing any operations on our databases</strong>. Regardless of how widely used, or well-tested the tool is, something can always go wrong and we have to be ready to roll out plan B.</p>
<h3 class="article-heading-introduction">3.1 Add Flyway Dependency</h3>
<p>As the first thing (after the backup!), let&#8217;s add the necessary dependency to our Spring Boot project.</p>
<p>For gradle, it will look as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="groovy">implementation 'org.flywaydb:flyway-core:9.8.3'</pre>
<p>On the other hand, when working with Maven, we need to add these lines:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="xml">&lt;dependency&gt;
  &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
  &lt;artifactId&gt;flyway-core&lt;/artifactId&gt;
  &lt;version&gt;9.8.3&lt;/version&gt;
&lt;/dependency&gt;</pre>
<h3 class="article-heading-introduction">3.2 Verify Spring Boot App</h3>
<p>Nextly, let&#8217;s run our Spring Boot application and verify, whether anything changed:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Found non-empty schema(s) "custom_schema" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[spring-beans-6.0.2.jar:6.0.2]</pre>
<p>As we can clearly see, Flyway throws a pretty descriptive message:</p>
<blockquote><p>Found non-empty schema(s) &#8220;custom_schema&#8221; but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.</p></blockquote>
<p>And basically, this error clearly states what we need to do when adding a Flyway to an existing project. But before that, we have to take care of a couple of things.</p>
<h3 class="article-heading-introduction">3.3 Generate Production Database DDL</h3>
<p>Firstly, we have to generate an <strong>SQL script containing the DDL our production database</strong>, including:</p>
<ul>
<li>procedures</li>
<li>views,</li>
<li>triggers etc&#8230;</li>
</ul>
<p>I won&#8217;t go into details here, because DDL script generation depends on the database you are using. Nevertheless, it can be done pretty easily with tools like <em>DataGrip</em>, <em>DBeaver</em>, or <em>pg_dump</em>.<br />
<a href="https://codersee.com/newsletter/"><img loading="lazy" decoding="async" class="aligncenter wp-image-3002956 size-large" src="http://blog.codersee.com/wp-content/uploads/2022/05/join_newsletter-1024x576.png" alt="Image shows two ebooks people can get for free after joining newsletter" width="800" height="419" /></a></p>
<p>And when the SQL script with DDL is ready, let&#8217;s save it in the Flyway migrations directory (in Spring Boot the default one is <em>resources/db/migration)</em>. Of course, we have to remember about the Flyway files naming convention, so the file name should be something, like <em>V1__my_custom_init_script.sql</em>.</p>
<h3 class="article-heading-introduction">3.4 Clear Irrelevant Data</h3>
<p>If you have only one database instance in your project, then feel free to skip this point. Otherwise,<strong> we have to make sure that the generated DDL script will work in every other environment</strong>. And we can achieve it in two ways:</p>
<ul>
<li><strong>if we don&#8217;t care about the data</strong> in other environments- then we can run the Flyway <em>clean</em> command.</li>
<li>however, <strong>if we do actually care about the data</strong> in our non-production environments, then there&#8217;s no other choice than to manually verify and align other database structures with the main one.</li>
</ul>
<p>The choice here is yours and will depend on many factors, like the importance of test data, amount of environments, etc.</p>
<p>Nevertheless, if you would like to clean your database before adding a Flyway to your existing project, then you can do that with the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">flyway -defaultSchema="custom_schema" \
  -url="jdbc:postgresql://{URL}:{PORT}/{DB_NAME}" \
  -cleanDisabled="false" \
  -user="{USERNAME}" \
  clean</pre>
<p>Please keep in mind that with this command, we will be prompted to enter the password manually (and alternatively, we can pass the <em>-password</em> flag).</p>
<h3 class="article-heading-introduction">3.5 Add Flyway To Existing Spring Boot Project</h3>
<p>Finally, with all that being done, we can return to the Spring Boot project and make the necessary changes.</p>
<p>Let&#8217;s navigate to the <strong>application.yaml</strong> file and insert these settings:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">spring:
  flyway:
    baselineOnMigrate: true
    defaultSchema: "custom_schema" # not necessary, but most likely you'll want to point to a specific schema</pre>
<p>As we can see, with <strong>baselineOnMigrate</strong> set to true, we instruct Flyway to <strong>automatically call baseline when migration is executed against a non-empty schema</strong>. Additionally, we set the <strong>defaultSchema</strong> value, but this is totally optional.</p>
<p>As a result, the <strong>flyway_schema_history</strong> table is added to our schema:</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-5504072 size-full" src="http://blog.codersee.com/wp-content/uploads/2022/12/flyway_schema_history_table_structure.png" alt="Image presents screenshot with flyway_schema_history table structure, which was added when we add Flyway to existing Spring Boot project." width="837" height="218" srcset="https://blog.codersee.com/wp-content/uploads/2022/12/flyway_schema_history_table_structure.png 837w, https://blog.codersee.com/wp-content/uploads/2022/12/flyway_schema_history_table_structure-300x78.png 300w, https://blog.codersee.com/wp-content/uploads/2022/12/flyway_schema_history_table_structure-768x200.png 768w" sizes="auto, (max-width: 837px) 100vw, 837px" /></p>
<p>and populated with <strong>precisely one record</strong>.</p>
<p>Please remember that the <strong>baselineOnMigrate set to true is only required once- when we do the initial deployment</strong> and can be removed after that.</p>
<h3 class="article-heading-introduction">3.6 Add Flyway Using Commands</h3>
<p>Basically, with all of the above being done we are finished and can enjoy the benefits of Flyway in our project.</p>
<p>However, if you would like to learn how to add Flyway to an existing project using CLI, then you can do exactly the same with the following command:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">flyway -defaultSchema="custom_schema" \ 
-url="jdbc:postgresql://{HOST}:{PORT}/{DB_NAME}" \
-user="USERNAME" \
-baselineOnMigrate="true" \
-locations="filesystem:." migrate</pre>
<p>Or, alternatively<strong>, run baseline only: </strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">flyway -defaultSchema="custom_schema" \ 
-url="jdbc:postgresql://{HOST}:{PORT}/{DB_NAME}" \
-user="USERNAME" \
-locations="filesystem:." baseline</pre>
<p>And then <strong>the migrate command</strong>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">flyway -defaultSchema="custom_schema" \ 
-url="jdbc:postgresql://{HOST}:{PORT}/{DB_NAME}" \
-user="USERNAME" \
-locations="filesystem:." migrate</pre>
<p>These commands might be useful if you would like to extract migrations to a separate directory (and/or repository).</p>
<h2>4. Add Flyway To An Existing Spring Boot Project Summary</h2>
<p>And that would be all in this article about how to add Flyway to an existing Spring Boot project. If you&#8217;d like to get to know Flyway better, then I highly encourage you to visit <a href="https://flywaydb.org/documentation/" target="_blank" rel="noopener">their documentation</a>.</p>
<p>Let me know in the comments section in case of any questions 🙂</p>
<p>The post <a href="https://blog.codersee.com/add-flyway-to-existing-spring-boot-project/">Add Flyway To An Existing Spring Boot Project</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/add-flyway-to-existing-spring-boot-project/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: blog.codersee.com @ 2026-05-13 10:07:35 by W3 Total Cache
-->