<?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>ji-ka</title>
	<atom:link href="https://www.ji-ka.tn/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ji-ka.tn/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Dec 2024 12:06:11 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://www.ji-ka.tn/wp-content/uploads/2023/10/cropped-icon-32x32.png</url>
	<title>ji-ka</title>
	<link>https://www.ji-ka.tn/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Développer un Plugin Maven</title>
		<link>https://www.ji-ka.tn/create-maven-plugin/</link>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Sat, 14 Dec 2024 12:01:55 +0000</pubDate>
				<category><![CDATA[spring]]></category>
		<category><![CDATA[actuator]]></category>
		<category><![CDATA[maven]]></category>
		<guid isPermaLink="false">https://www.ji-ka.tn/?p=1199</guid>

					<description><![CDATA[<p>Maven est un outil puissant d&#8217;automatisation de la construction utilisé principalement pour les projets Java. Une caractéristique clé de Maven est son extensibilité via des plugins, qui permettent de personnaliser et d&#8217;améliorer le processus de construction. Dans ce guide, vous apprendrez à créer un plugin Maven personnalisé. Prérequis Avant de commencer, assurez-vous d&#8217;avoir : Étape [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/create-maven-plugin/">Développer un Plugin Maven</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Maven est un outil puissant d&rsquo;automatisation de la construction utilisé principalement pour les projets Java. Une caractéristique clé de Maven est son extensibilité via des plugins, qui permettent de personnaliser et d&rsquo;améliorer le processus de construction. Dans ce guide, vous apprendrez à créer un plugin Maven personnalisé.</p>



<span id="more-1199"></span>



<h2 class="wp-block-heading">Prérequis</h2>



<p>Avant de commencer, assurez-vous d&rsquo;avoir :</p>



<ul class="wp-block-list">
<li><strong>Java Development Kit (JDK)</strong> installé.</li>



<li><strong>Apache Maven</strong> installé et configuré.</li>



<li>Une connaissance de base des concepts Java et Maven.</li>
</ul>



<h2 class="wp-block-heading">Étape 1 : Configurer le Projet du Plugin</h2>



<p>Pour créer un plugin Maven, commencez par configurer un nouveau projet Maven :</p>



<ol class="wp-block-list">
<li>Ouvrez un terminal ou un IDE et exécutez la commande suivante pour créer une structure de projet :</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * 1.125rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="mvn archetype:generate -DgroupId=com.exemple -DartifactId=my-plugin -DarchetypeArtifactId=maven-archetype-plugin -DinteractiveMode=false" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">mvn archetype:generate </span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">DgroupId</span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6">com.exemple </span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">DartifactId</span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6">my</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">plugin </span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">DarchetypeArtifactId</span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6">maven</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">archetype</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">plugin </span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">DinteractiveMode</span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6">false</span></span></code></pre></div>



<p></p>



<p>Cette commande utilise l&rsquo;archétype de plugin Maven pour générer une structure de base pour votre plugin.</p>



<ol start="2" class="wp-block-list">
<li>Accédez au répertoire du projet généré</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="cd my-plugin" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">cd my</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">plugin</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">Étape 2 : Implémenter la Logique du Plugin</h2>



<ol class="wp-block-list">
<li>Localisez la classe principale du plugin sous <code>src/main/java/com/example/MyMojo.java</code>. Cette classe est annotée avec <code>@Mojo</code> et sert de point d&rsquo;entrée pour votre plugin.</li>
</ol>



<ol start="2" class="wp-block-list">
<li>Modifiez la classe <code>MyMojo</code> pour implémenter la fonctionnalité souhaitée. Par exemple :</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="package com.example;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@Mojo(name = &quot;generatefile&quot;)
public class MyMojo extends AbstractMojo {

    @Parameter(property = &quot;outputDir&quot;, defaultValue = &quot;${project.build.directory}&quot;)
    private File outputDir;

    @Parameter(property = &quot;filename&quot;, defaultValue = &quot;output.txt&quot;)
    private String filename;

    @Parameter(property = &quot;content&quot;, defaultValue = &quot;Hello, Maven Plugin!&quot;)
    private String content;

    public void execute() throws MojoExecutionException {
        File outputFile = new File(outputDir, filename);
        try {
            if (!outputDir.exists()) {
                outputDir.mkdirs();
            }
            try (FileWriter writer = new FileWriter(outputFile)) {
                writer.write(content);
            }
            getLog().info(&quot;Fichier généré à : &quot; + outputFile.getAbsolutePath());
        } catch (IOException e) {
            throw new MojoExecutionException(&quot;Erreur lors de la génération du fichier&quot;, e);
        }
    }
}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">package</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">com.example</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">org.apache.maven.plugin.AbstractMojo</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">org.apache.maven.plugin.MojoExecutionException</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">org.apache.maven.plugins.annotations.Mojo</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">org.apache.maven.plugins.annotations.Parameter</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">java.io.File</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">java.io.FileWriter</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #569CD6">import</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">java.io.IOException</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Mojo</span><span style="color: #E6E6E6">(name </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;generatefile&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">MyMojo</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">extends</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">AbstractMojo</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Parameter</span><span style="color: #E6E6E6">(property </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;outputDir&quot;</span><span style="color: #E6E6E6">, defaultValue </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;${project.build.directory}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">File</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">outputDir</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Parameter</span><span style="color: #E6E6E6">(property </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;filename&quot;</span><span style="color: #E6E6E6">, defaultValue </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;output.txt&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">filename</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Parameter</span><span style="color: #E6E6E6">(property </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;content&quot;</span><span style="color: #E6E6E6">, defaultValue </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;Hello, Maven Plugin!&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">content</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">void</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">execute</span><span style="color: #E6E6E6">() </span><span style="color: #569CD6">throws</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">MojoExecutionException</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #4EC9B0">File</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">outputFile</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">File</span><span style="color: #E6E6E6">(outputDir, filename);</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #C586C0">try</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #C586C0">if</span><span style="color: #E6E6E6"> (</span><span style="color: #D4D4D4">!</span><span style="color: #9CDCFE">outputDir</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">exists</span><span style="color: #E6E6E6">()) {</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #9CDCFE">outputDir</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">mkdirs</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">            }</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #C586C0">try</span><span style="color: #E6E6E6"> (</span><span style="color: #4EC9B0">FileWriter</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">writer</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">FileWriter</span><span style="color: #E6E6E6">(outputFile)) {</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #9CDCFE">writer</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">write</span><span style="color: #E6E6E6">(content);</span></span>
<span class="line"><span style="color: #E6E6E6">            }</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #DCDCAA">getLog</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">info</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;Fichier généré à : &quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">+</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">outputFile</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getAbsolutePath</span><span style="color: #E6E6E6">());</span></span>
<span class="line"><span style="color: #E6E6E6">        } </span><span style="color: #C586C0">catch</span><span style="color: #E6E6E6"> (</span><span style="color: #4EC9B0">IOException</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">e</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #C586C0">throw</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">MojoExecutionException</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;Erreur lors de la génération du fichier&quot;</span><span style="color: #E6E6E6">, e);</span></span>
<span class="line"><span style="color: #E6E6E6">        }</span></span>
<span class="line"><span style="color: #E6E6E6">    }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<p>Dans cet exemple, le plugin définit une tâche nommée <code>generatefile</code> qui génère un fichier texte avec le contenu spécifié. Les utilisateurs peuvent personnaliser le répertoire de sortie, le nom du fichier et le contenu à l&rsquo;aide des paramètres du plugin.</p>



<ol start="1" class="wp-block-list">
<li></li>
</ol>



<h2 class="wp-block-heading">Étape 3 : Construire et Tester le Plugin</h2>



<ol class="wp-block-list">
<li>Construisez le projet du plugin en exécutant</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="mvn clean install" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">mvn </span><span style="color: #C586C0">clean</span><span style="color: #E6E6E6"> install</span></span></code></pre></div>



<p></p>



<p>Cette commande compile le plugin et l&rsquo;installe dans votre dépôt Maven local.</p>



<ol start="2" class="wp-block-list">
<li>Pour tester le plugin, créez un projet Maven simple et ajoutez la configuration suivante à son fichier <code>pom.xml</code> :</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;com.example&lt;/groupId&gt;
            &lt;artifactId&gt;my-plugin&lt;/artifactId&gt;
            &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
            &lt;configuration&gt;
                &lt;outputDir&gt;${project.build.directory}/custom-dir&lt;/outputDir&gt;
                &lt;filename&gt;custom-file.txt&lt;/filename&gt;
                &lt;content&gt;Ceci est un fichier généré sur mesure.&lt;/content&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">build</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">plugins</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">plugin</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">com.example</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">my-plugin</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">version</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">1.0-SNAPSHOT</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">version</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">configuration</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">outputDir</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">${project.build.directory}/custom-dir</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">outputDir</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">filename</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">custom-file.txt</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">filename</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">content</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">Ceci est un fichier généré sur mesure.</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">content</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">configuration</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">plugin</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">plugins</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">build</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p></p>



<ol start="3" class="wp-block-list">
<li>Exécutez le plugin avec la commande :</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="mvn com.example:my-plugin:1.0-SNAPSHOT:generatefile" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">mvn com.example:my</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">plugin:</span><span style="color: #B5CEA8">1.0</span><span style="color: #D4D4D4">-</span><span style="color: #E6E6E6">SNAPSHOT:generatefile</span></span></code></pre></div>



<p></p>



<p>Vous devriez voir un message indiquant l&#8217;emplacement du fichier. Vérifiez que le fichier est créé avec le contenu spécifié.</p>



<h2 class="wp-block-heading">Étape 4 : Personnaliser et Étendre</h2>



<p>Vous pouvez améliorer votre plugin en ajoutant plus de tâches, de paramètres et de dépendances. Voici quelques conseils :</p>



<ul class="wp-block-list">
<li><strong>Ajouter Plusieurs Tâches</strong> : Définissez plusieurs classes annotées avec <code>@Mojo</code>, chacune implémentant une tâche différente.</li>



<li><strong>Accéder aux Détails du Projet</strong> : Utilisez l&rsquo;annotation <code>@Parameter</code> pour injecter les détails du projet Maven, tels que les dépendances et les répertoires de construction.</li>



<li><strong>Inclure des Dépendances</strong> : Ajoutez des dépendances dans le fichier <code>pom.xml</code> de votre plugin pour réutiliser des bibliothèques existantes.</li>
</ul>



<h2 class="wp-block-heading">Étape 5 : Publier le Plugin (Optionnel)</h2>



<p>Pour rendre votre plugin disponible à d&rsquo;autres développeurs, envisagez de le publier dans un dépôt Maven. Mettez à jour le fichier <code>pom.xml</code> avec les paramètres de gestion de distribution et déployez le plugin en exécutant :</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="mvn deploy" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">mvn deploy</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Créer un plugin Maven personnalisé peut considérablement simplifier vos processus de construction et ajouter des fonctionnalités précieuses à vos projets. En suivant ce guide, vous pouvez construire, tester et étendre votre plugin facilement. Explorez la documentation Maven pour découvrir des fonctionnalités avancées et des meilleures pratiques.</p>



<h2 class="wp-block-heading">Code source</h2>



<p>L’exemple complet de ce tutorial est disponible sur&nbsp;<a href="https://github.com/JKaouech/spring-boot/tree/master/dependencies-actuator-maven-plugin">GitHub</a></p>
<p>L’article <a href="https://www.ji-ka.tn/create-maven-plugin/">Développer un Plugin Maven</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Cycle de Vie des Beans dans Spring Framework</title>
		<link>https://www.ji-ka.tn/cycle-de-vie-des-beans-dans-spring-framework/</link>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Wed, 04 Oct 2023 20:35:07 +0000</pubDate>
				<category><![CDATA[spring]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=1126</guid>

					<description><![CDATA[<p>On utilise beaucoup Spring et Spring Boot, mais parfois on ne sait pas comment cela fonctionne en interne. Ces mécanismes n&#8217;ont pas changés et toutes les fonctionnalités de Spring reposent dessus. 1. Spring, c&#8217;est quoi Sorti en 2003, Spring est un Framework de développement d&#8217;application Java qui fournit une infrastructure complète pour le développement d&#8217;applications [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/cycle-de-vie-des-beans-dans-spring-framework/">Cycle de Vie des Beans dans Spring Framework</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>On utilise beaucoup Spring et Spring Boot, mais parfois on ne sait pas comment cela fonctionne en interne. Ces mécanismes n&rsquo;ont pas changés et toutes les fonctionnalités de Spring reposent dessus.</p>



<span id="more-1126"></span>



<h2 class="wp-block-heading">1. Spring, c&rsquo;est quoi</h2>



<p>Sorti en 2003, Spring est un Framework de développement d&rsquo;application Java qui fournit une infrastructure complète pour le développement d&rsquo;applications </p>



<p>Spring s’appuie principalement sur l’intégration de 3 concepts clés :</p>



<ul class="wp-block-list">
<li>L’inversion de contrôle&nbsp;: La recherche de dépendance et l’injection de dépendances</li>



<li>La programmation orientée aspect.</li>



<li>Une&nbsp;couche d’abstraction qui permet d’intégrer d’autres Framework et bibliothèques.</li>
</ul>



<h2 class="wp-block-heading">2. Bean, c&rsquo;est quoi</h2>



<p>Un Bean est une instance d&rsquo;une classe qui est crée, assemblé et géré par le conteneur Spring offrant ainsi un moyen souple de gérer les composants d&rsquo;une application.</p>



<h3 class="wp-block-heading">2.1. Caractéristiques des Beans</h3>



<ul class="wp-block-list">
<li><strong>Gestion du cycle de vie :</strong> Spring gère le cycle de vie des Beans, depuis leur création jusqu&rsquo;à leur destruction.</li>



<li><strong>Injection de dépendances :</strong> Les dépendances d&rsquo;un Bean peuvent être injectées par le conteneur Spring, ce qui facilite le développement et la maintenance de l&rsquo;application.</li>



<li><strong>Configuration déclarative :</strong> Les Beans peuvent être configurés à l&rsquo;aide de fichiers de configuration XML, d&rsquo;annotations Java ou de configurations basées sur Java.</li>



<li><strong>Réutilisation et modularité :</strong> Les beans peuvent être réutilisés et assemblés pour former des applications modulaires.</li>
</ul>



<h3 class="wp-block-heading">2.2. Cycle de vie d&rsquo;un Bean</h3>



<p>Le cycle de vie des beans dans Spring se compose de plusieurs étapes distinctes, chacune jouant un rôle crucial dans le bon fonctionnement de l&rsquo;application.  :</p>



<p>Un Bean suit un cycle de vie bien défini, composé des phases clés suivantes :</p>



<ul class="wp-block-list">
<li><strong>Instanciation :</strong> Cette phase correspond à la création d&rsquo;une instance de bean. Spring prend en charge plusieurs mécanismes d&rsquo;instanciation, tels que la création par un constructeur, la création par une méthode statique de la classe, ou la récupération depuis un pool de beans déjà créés.</li>



<li><strong>Injection des Dépendances</strong> : Une fois que le bean est créé, Spring injecte les dépendances nécessaires. Cela peut être réalisé par le biais de méthodes de configuration (setter) ou de la configuration du constructeur.</li>



<li><strong>Initialisation :</strong> Après l&rsquo;injection des dépendances, le conteneur Spring appelle des méthodes d&rsquo;initialisation spécifiées par le développeur. Ces méthodes permettent d&rsquo;effectuer des opérations personnalisées avant que le bean ne soit pleinement opérationnel.</li>



<li><strong>Utilisation :</strong> À ce stade, le bean est prêt à être utilisé dans l&rsquo;application. Les autres composants de l&rsquo;application peuvent maintenant interagir avec le bean pour effectuer leurs tâches.</li>



<li><strong>Destruction :</strong> Lorsque l&rsquo;application se termine ou lorsque le bean n&rsquo;est plus nécessaire, le conteneur Spring peut appeler des méthodes de destruction spécifiées par le développeur. Ces méthodes permettent d&rsquo;effectuer des opérations de nettoyage ou de libération de ressources.</li>
</ul>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="847" height="382" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/10/Spring-bean-cdv-1.png" alt="" class="wp-image-1128" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/10/Spring-bean-cdv-1.png 847w, https://www.ji-ka.tn/wp-content/uploads/2023/10/Spring-bean-cdv-1-300x135.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/10/Spring-bean-cdv-1-768x346.png 768w" sizes="(max-width: 847px) 100vw, 847px" /><figcaption class="wp-element-caption">étape d&rsquo;initialisation du bean</figcaption></figure>



<p></p>



<h2 class="wp-block-heading">3. Plus de d&rsquo;étails</h2>



<p>Carl Azoury  nous montre, ici, comment fonctionne le cycle de vie des Beans via cette conférences&nbsp; qui a eu lieu lors de l&rsquo;événement DEVOXX France 2023</p>



<p></p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<p class="responsive-video-wrap clr"><iframe title="Les secrets internes de Spring (Carl Azoury)" width="1200" height="675" src="https://www.youtube.com/embed/P61kCoXvKIc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe></p>
</div></figure>



<p></p>



<h2 class="wp-block-heading">4. Conclusion</h2>



<p>Comprendre le cycle de vie des beans dans Spring est essentiel pour un développement efficace et une gestion appropriée des ressources. En utilisant les différentes annotations et interfaces fournies par Spring, vous pouvez contrôler le comportement de vos beans à chaque étape de leur cycle de vie, garantissant ainsi une application bien gérée et performante</p>
<p>L’article <a href="https://www.ji-ka.tn/cycle-de-vie-des-beans-dans-spring-framework/">Cycle de Vie des Beans dans Spring Framework</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Premier pas avec Spring et ChatGPT</title>
		<link>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/</link>
					<comments>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/#respond</comments>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Tue, 03 Oct 2023 20:40:44 +0000</pubDate>
				<category><![CDATA[spring boot]]></category>
		<category><![CDATA[chatgpt]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=1106</guid>

					<description><![CDATA[<p>Dans ce tutoriel, nous allons découvrir l&#8217;api ChatGPT d&#8217;openAI. D&#8217;abord, on va voir comment tester l&#8217;api ChatGPT et comprendre son model de donnée. Ensuite, nous allons créer une application Spring Boot qui génère des réponses à partir de cette API. 1 API ChatGPT l&#8217;API qu&#8217;on va utiliser dans ce tutorial s&#8217;appelle chat completation. Commençant par [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/">Premier pas avec Spring et ChatGPT</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Dans ce tutoriel, nous allons découvrir l&rsquo;api ChatGPT d&rsquo;openAI. D&rsquo;abord, on va voir comment tester l&rsquo;api ChatGPT et comprendre son model de donnée. Ensuite, nous allons créer une application Spring Boot qui génère des réponses à partir de cette API.</p>



<span id="more-1106"></span>



<h2 class="wp-block-heading">1 API ChatGPT</h2>



<p>l&rsquo;API qu&rsquo;on va utiliser dans ce tutorial s&rsquo;appelle <a href="https://platform.openai.com/docs/api-reference/chat/create">chat completation</a>. Commençant par jeter un coup d&rsquo;œil à cette API pour comprendre son modèle.</p>



<h3 class="wp-block-heading">1.1 Accès à l&rsquo;API</h3>



<p>Pour utiliser l&rsquo;API <a href="https://platform.openai.com/docs/api-reference/chat/create">chat completation</a>, vous devez obtenir un accès auprès d&rsquo;<a href="https://platform.openai.com/account/api-keys">OpenAI</a></p>



<figure class="wp-block-image size-full is-style-default"><img decoding="async" width="909" height="595" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/10/cle-API-OpenAI.png" alt="" class="wp-image-1107" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI.png 909w, https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI-300x196.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI-768x503.png 768w" sizes="(max-width: 909px) 100vw, 909px" /><figcaption class="wp-element-caption">Configuration Clé ChatGPT</figcaption></figure>



<h3 class="wp-block-heading">1.2 <strong>Utilisation de l&rsquo;AP</strong>I</h3>



<p>Utilisez la clé API pour faire des requêtes à l&rsquo;API « <em>completions</em> » en utilisant des appels HTTP. Il faut fournir un message qui est le texte d&rsquo;entrée ou la question que vous souhaitez poser et l&rsquo;API renvoie une réponse générée par le moteur GPT.</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:0;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="curl -X POST -s https://api.openai.com/v1/chat/completions \
-H &quot;Content-Type: application/json&quot; \
-H &quot;Authorization: Bearer ${CLE_API}&quot; \
-d '{
    &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
    &quot;messages&quot;: [
        {
            &quot;role&quot;: &quot;user&quot;,
            &quot;content&quot;: &quot;Hello!&quot;
        }
    ]
}'" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">curl</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">-X</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">POST</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">-s</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">https://api.openai.com/v1/chat/completions</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-H </span><span style="color: #CE9178">&quot;Content-Type: application/json&quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-H </span><span style="color: #CE9178">&quot;Authorization: Bearer ${</span><span style="color: #9CDCFE">CLE_API</span><span style="color: #CE9178">}&quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-d </span><span style="color: #CE9178">&#39;{</span></span>
<span class="line"><span style="color: #CE9178">    &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,</span></span>
<span class="line"><span style="color: #CE9178">    &quot;messages&quot;: [</span></span>
<span class="line"><span style="color: #CE9178">        {</span></span>
<span class="line"><span style="color: #CE9178">            &quot;role&quot;: &quot;user&quot;,</span></span>
<span class="line"><span style="color: #CE9178">            &quot;content&quot;: &quot;Hello!&quot;</span></span>
<span class="line"><span style="color: #CE9178">        }</span></span>
<span class="line"><span style="color: #CE9178">    ]</span></span>
<span class="line"><span style="color: #CE9178">}&#39;</span></span></code></pre></div>



<p></p>



<p>Assurez vous de remplacer « ${CLE_API} » par votre clé API réelle et de personnaliser les autres paramètres de la requête en fonction de vos besoins.</p>



<ul class="wp-block-list">
<li><strong><em>model </em></strong>: ID / version du modèle à utiliser. Pour cet exemple, on va utilisée le denier model de la version publique  <em>(gpt-3.5-turbo)</em>, ceci dit, il existe d&rsquo;autres models aussi, <a href="https://platform.openai.com/docs/models/model-endpoint-compatibility">voir ici</a>.</li>



<li><em><strong>messages</strong></em> : Une liste de messages comprenant la conversation. Chaque message nécessite deux champs:
<ul class="wp-block-list">
<li>role : spécifie l&rsquo;expéditeur du message</li>



<li>content: le message réel à envoyer</li>
</ul>
</li>
</ul>



<h3 class="wp-block-heading">1.3 Réponse de l&rsquo;API</h3>



<p>La réponse de l&rsquo;API sera au format JSON avec des métadonnées et un&nbsp; champ&nbsp;« <em>choices</em>« . Ce champ est une liste qui contient un index et une liste de messages.</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 role="button" tabindex="0" data-code="{
  &quot;id&quot;: &quot;chatcmpl-123&quot;,
  &quot;object&quot;: &quot;chat.completion&quot;,
  &quot;created&quot;: 1589478378,
  &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
  &quot;choices&quot;: [{
    &quot;index&quot;: 0,
    &quot;message&quot;: {
      &quot;role&quot;: &quot;assistant&quot;,
      &quot;content&quot;: &quot;\n\nHello there, how may I assist you today?&quot;,
    },
    &quot;finish_reason&quot;: &quot;stop&quot;
  }],
  &quot;usage&quot;: {
    &quot;prompt_tokens&quot;: 5,
    &quot;completion_tokens&quot;: 7,
    &quot;total_tokens&quot;: 12
  }
}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">{</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;id&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;chatcmpl-123&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;object&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;chat.completion&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;created&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1589478378</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;model&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;gpt-3.5-turbo&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;choices&quot;</span><span style="color: #E6E6E6">: [{</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;index&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">0</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;message&quot;</span><span style="color: #E6E6E6">: {</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #9CDCFE">&quot;role&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;assistant&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #9CDCFE">&quot;content&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;</span><span style="color: #D7BA7D">\n\n</span><span style="color: #CE9178">Hello there, how may I assist you today?&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    },</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;finish_reason&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;stop&quot;</span></span>
<span class="line"><span style="color: #E6E6E6">  }],</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;usage&quot;</span><span style="color: #E6E6E6">: {</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;prompt_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">5</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;completion_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">7</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;total_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">12</span></span>
<span class="line"><span style="color: #E6E6E6">  }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">2 Spring et ChatGPT</h2>



<p>Nous allons créer une application Spring Boot contenant une API Rest qui accepte un message dans les paramètres de la requête, le transmet à l&rsquo;API ChatGPT et renvoie le message de retour.</p>



<h3 class="wp-block-heading">2.1 Dépendances</h3>



<p>Créer un projet du starter Spring boot et ajouter la dépendance « <a href="https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web">spring-boot-starter-web</a>« </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 role="button" tabindex="0" data-code="		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p></p>



<p>Aussi, on a besoin de Lombok pour faciliter la génération du getter et setter. </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 role="button" tabindex="0" data-code="		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.projectlombok</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">lombok</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">optional</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">true</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">optional</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.2 Configuration</h3>



<p>Dans cette article on va faire simple du coup on va utiliser ResTemplate pour s&rsquo;interfacer avec OpenAI. On doit ajouter un intercepteur pour ajouter la clé.</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 role="button" tabindex="0" data-code="@Configuration
public class OpenAIConfig {

    @Value(&quot;${application.openai.key}&quot;)
    private String openaiApiKey;

    @Bean
    @Qualifier(&quot;openaiRestTemplate&quot;)
    public RestTemplate openaiRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add((request, body, execution) -&gt; {
            request.getHeaders().add(&quot;Authorization&quot;, &quot;Bearer &quot; + openaiApiKey);
            return execution.execute(request, body);
        });
        return restTemplate;
    }
}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Configuration</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">OpenAIConfig</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.key}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">openaiApiKey</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Bean</span></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Qualifier</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;openaiRestTemplate&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">openaiRestTemplate</span><span style="color: #E6E6E6">() {</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">RestTemplate</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getInterceptors</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">((request, body, execution) </span><span style="color: #569CD6">-&gt;</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #9CDCFE">request</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getHeaders</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;Authorization&quot;</span><span style="color: #E6E6E6">, </span><span style="color: #CE9178">&quot;Bearer &quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">+</span><span style="color: #E6E6E6"> openaiApiKey);</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">execution</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">execute</span><span style="color: #E6E6E6">(request, body);</span></span>
<span class="line"><span style="color: #E6E6E6">        });</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> restTemplate;</span></span>
<span class="line"><span style="color: #E6E6E6">    }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.3 Modèle</h3>



<p>Créons maintenant le modèle correspondant au modèle de l&rsquo;api ChatGPT.</p>



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



<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 role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {

		private String role;
		private String content;

}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">role</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">content</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



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



<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 role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class Choice {

    private int index;
    private Message message;
}
" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Choice</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">int</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">index</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p>Ensuite, créons note model request et response.</p>



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



<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 role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatRequest {

	private String model;
	private List&lt;Message&gt; messages;

	@Builder
	public ChatRequest(String model, String message) {
		this.model = model;
		this.messages = new ArrayList&lt;&gt;();
		this.messages.add(new Message(&quot;user&quot;, message));
	}

}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatRequest</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">List</span><span style="color: #E6E6E6">&lt;</span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6">&gt; </span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Builder</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">ChatRequest</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">, </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> model;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ArrayList</span><span style="color: #E6E6E6">&lt;&gt;();</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">(</span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">Message</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;user&quot;</span><span style="color: #E6E6E6">, message));</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Classe  de réponse :</li>
</ul>



<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 role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatResponse {

	private String id;
	private List&lt;Choice&gt; choices;
	@JsonProperty(&quot;finish_reason&quot;)
	private String finishReason;
}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatResponse</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">id</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">List</span><span style="color: #E6E6E6">&lt;</span><span style="color: #4EC9B0">Choice</span><span style="color: #E6E6E6">&gt; </span><span style="color: #9CDCFE">choices</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">JsonProperty</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;finish_reason&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">finishReason</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.4 Service</h3>



<p>La classe service permet d&rsquo;appeler notre client OpenAI :</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 role="button" tabindex="0" data-code="@Service
@RequiredArgsConstructor
public class ChatService {

	@Qualifier(&quot;openaiRestTemplate&quot;)
	private final RestTemplate restTemplate;

	@Value(&quot;${application.openai.model}&quot;)
	private String model;

	@Value(&quot;${application.openai.url}&quot;)
	private String apiUrl;

	public String sendMessage(String message) {
		ChatRequest request = ChatRequest.builder()
				.model(model)
				.message(message)
				.build();
				
		ChatResponse response = restTemplate.postForObject(apiUrl, request, ChatResponse.class);
		if (response == null || CollectionUtils.isEmpty(response.getChoices())) {
			return &quot;No response&quot;;
		}
		return response.getChoices().get(0).getMessage().getContent();
	}

}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Service</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RequiredArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatService</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Qualifier</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;openaiRestTemplate&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">final</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.model}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.url}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">apiUrl</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">sendMessage</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">ChatRequest</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">request</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">ChatRequest</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">builder</span><span style="color: #E6E6E6">()</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">model</span><span style="color: #E6E6E6">(model)</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">message</span><span style="color: #E6E6E6">(message)</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">build</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">				</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">ChatResponse</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">postForObject</span><span style="color: #E6E6E6">(apiUrl, request, </span><span style="color: #9CDCFE">ChatResponse</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">class</span><span style="color: #E6E6E6">);</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">if</span><span style="color: #E6E6E6"> (response </span><span style="color: #D4D4D4">==</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">null</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">||</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">CollectionUtils</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">isEmpty</span><span style="color: #E6E6E6">(</span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getChoices</span><span style="color: #E6E6E6">())) {</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;No response&quot;</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">		}</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getChoices</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">get</span><span style="color: #E6E6E6">(</span><span style="color: #B5CEA8">0</span><span style="color: #E6E6E6">).</span><span style="color: #DCDCAA">getMessage</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">getContent</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.5 Api d&rsquo;entré</h3>



<p>Exposant maintenant notre Api :</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 role="button" tabindex="0" data-code="@RestController
@RequiredArgsConstructor
public class ChatController {

	private final ChatService chatService;

	@GetMapping(&quot;/chat&quot;)
	public String chat(@RequestParam String message) {
		return chatService.sendMessage(message);
	}

}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RestController</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RequiredArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatController</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">final</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatService</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">chatService</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">GetMapping</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;/chat&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">chat</span><span style="color: #E6E6E6">(@</span><span style="color: #4EC9B0">RequestParam</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">chatService</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">sendMessage</span><span style="color: #E6E6E6">(message);</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.6 Propriétés</h3>



<p>Ajoutons dans le fichier « <em>application.</em>yml » les paramètres qu&rsquo;on a déclaré.</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 role="button" tabindex="0" data-code="application:
  openai:
    model: gpt-3.5-turbo
    url: https://api.openai.com/v1/chat/completions
    key: ${your_api_key}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">application</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">openai</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">model</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">gpt-3.5-turbo</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">url</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">https://api.openai.com/v1/chat/completions</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">key</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${your_api_key}</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">3 Exécution</h2>



<p>Désormé, on peut lancer l&rsquo;application et appeler notre api via<a href="http://localhost:8080/chat?message=Hello!"> http://localhost:8080/chat?message=Hello!</a></p>



<h2 class="wp-block-heading">4 Code source</h2>



<p>L&rsquo;exemple complet de ce tutorial est disponible sur <a href="https://github.com/JKaouech/spring-boot/tree/master/spring-openai">GitHub</a></p>



<h2 class="wp-block-heading">5. Référence</h2>



<p><a href="https://platform.openai.com/docs/models/model-endpoint-compatibility">https://platform.openai.com/docs/models/model-endpoint-compatibility</a></p>



<p><a href="https://platform.openai.com/docs/api-reference/completions">https://platform.openai.com/docs/api-reference/completions</a></p>



<p></p>
<p>L’article <a href="https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/">Premier pas avec Spring et ChatGPT</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Monitoring with Prometheus and Grafana</title>
		<link>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/</link>
					<comments>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/#respond</comments>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Sat, 30 Sep 2023 11:00:44 +0000</pubDate>
				<category><![CDATA[spring boot]]></category>
		<category><![CDATA[grafana]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[prometheus]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=65</guid>

					<description><![CDATA[<p>In this article, we will be looking into how we can monitor our Spring Boot application using Grafana. We would be looking into the whole setup and create a simple dashboard to view some metrics. 1 Application Spring Boot is a very popular microservice framework that significantly simplifies web application development by providing Java developers [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/">Monitoring with Prometheus and Grafana</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will be looking into how we can monitor our Spring Boot application using Grafana. We would be looking into the whole setup and create a simple dashboard to view some metrics.</p>



<span id="more-65"></span>



<h3 class="wp-block-heading"><strong>1 Application</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Spring Boot is a very popular microservice framework that significantly simplifies web application development by providing Java developers with a platform to get started with an auto-configurable, production-grade Spring application.</p>



<h4 class="wp-block-heading"><strong>1.1 Actuator</strong></h4>



<p>Actuator endpoints let you monitor and interact with your Spring Boot application.<br>Read this article for more details : <a href="https://github.com/JKaouech/spring-boot/wiki/Spring-boot-actuator">spring boot actuator</a></p>



<h4 class="wp-block-heading"><strong>1.2 Setup</strong></h4>



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



<p>To expose prometeus enndpoint we nedd to add this 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 role="button" tabindex="0" data-code="        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
            &lt;artifactId&gt;micrometer-registry-prometheus&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
        &lt;/dependency&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-actuator</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">io.micrometer</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">micrometer-registry-prometheus</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">scope</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">runtime</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">scope</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<ul class="wp-block-list">
<li><strong>Spring Boot properties</strong></li>
</ul>



<p>Next, we need to expose an actuator endpoint through which Prometheus will collect metrics data in the format that Prometheus understands. For this, we need to add the following properties.</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 role="button" tabindex="0" data-code="# -- actuator global configuration -- #
management: 
  endpoints: 
    web: 
      exposure: 
        include: &quot;health,info,prometheus&quot;
  endpoint:
    health:
      show-details: always
    metrics:
      enabled: true
    prometheus:
      enabled: true 

management.metrics:
  tags:
    application: ${spring.application.name}
    env: ${spring.profiles.active}
  distribution:
    percentiles-histogram:
      http:
        server:
          requests: 'true'" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #6A9955"># -- actuator global configuration -- #</span></span>
<span class="line"><span style="color: #569CD6">management</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoints</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">web</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">exposure</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">include</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;health,info,prometheus&quot;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoint</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">health</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">show-details</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">always</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">metrics</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">prometheus</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span><span style="color: #E6E6E6"> </span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">management.metrics</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">tags</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">application</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${spring.application.name}</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">env</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${spring.profiles.active}</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">distribution</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">percentiles-histogram</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">http</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">server</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">          </span><span style="color: #569CD6">requests</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;true&#39;</span></span></code></pre></div>



<p>You can reach the info actuator on your local machine : <a href="http://localhost:8080/actuator/prometheus">http://localhost:8080/actuator/prometheus</a> once you start your Spring Boot application.</p>



<p>The prometheus endpoint shows various metrics, such as JVM threads state, information about HTTP server requests, etc.</p>



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



<p>After opening the above endpoint, you will find some metrics data in the following format</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 role="button" tabindex="0" data-code="jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Metaspace&quot;,} 4.417008E7
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Survivor Space&quot;,} 8388608.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap 'non-nmethods'&quot;,} 1294336.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Old Gen&quot;,} 9224992.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap 'non-profiled nmethods'&quot;,} 1.0788608E7
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Compressed Class Space&quot;,} 5484736.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Eden Space&quot;,} 2.5165824E7" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Metaspace&quot;,} 4.417008E7</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Survivor Space&quot;,} 8388608.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap &#39;non-nmethods&#39;&quot;,} 1294336.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Old Gen&quot;,} 9224992.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap &#39;non-profiled nmethods&#39;&quot;,} 1.0788608E7</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Compressed Class Space&quot;,} 5484736.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Eden Space&quot;,} 2.5165824E7</span></span></code></pre></div>



<p>The first part (<code>jvm_memory_used_bytes</code>) is called the label, while the fields inside the curly braces are called attributes. Each of these labels represents a particular metric and the attribute provides you with a way to query so that you can get the values.</p>



<h3 class="wp-block-heading"><strong>2 Prometheus</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Prometheus gathers metrics at intervals and needs to know how often to scrape them.<br>We will be using a Prometheus docker image and provide it with some configuration</p>



<h4 class="wp-block-heading"><strong>2.1 Configuration file</strong></h4>



<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 role="button" tabindex="0" data-code="# my global config
global:
  scrape_interval:     120s  # By default, scrape targets every 15 seconds.
  evaluation_interval: 120s  # By default, scrape targets every 15 seconds.

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:  
  - job_name: 'Spring Boot Application input'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 2s
    static_configs:
      - targets: ['host.docker.internal:8080']" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #6A9955"># my global config</span></span>
<span class="line"><span style="color: #569CD6">global</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">scrape_interval</span><span style="color: #E6E6E6">:     </span><span style="color: #CE9178">120s</span><span style="color: #E6E6E6">  </span><span style="color: #6A9955"># By default, scrape targets every 15 seconds.</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">evaluation_interval</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">120s</span><span style="color: #E6E6E6">  </span><span style="color: #6A9955"># By default, scrape targets every 15 seconds.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># A scrape configuration containing exactly one endpoint to scrape:</span></span>
<span class="line"><span style="color: #6A9955"># Here it&#39;s Prometheus itself.</span></span>
<span class="line"><span style="color: #569CD6">scrape_configs</span><span style="color: #E6E6E6">:  </span></span>
<span class="line"><span style="color: #E6E6E6">  - </span><span style="color: #569CD6">job_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;Spring Boot Application input&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">metrics_path</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;/actuator/prometheus&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">scrape_interval</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">2s</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">static_configs</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #569CD6">targets</span><span style="color: #E6E6E6">: [</span><span style="color: #CE9178">&#39;host.docker.internal:8080&#39;</span><span style="color: #E6E6E6">]</span></span></code></pre></div>



<p><strong>Important notes :</strong></p>



<ul class="wp-block-list">
<li><code>scrape_interval</code> : Scrape the application and collect information every 2 seconds</li>



<li><code>targets</code> : The target is the host and port of our application.</li>



<li><code>metrics_path</code> : The path we want to scrape.</li>
</ul>



<h4 class="wp-block-heading"><strong>2.2 Start service</strong></h4>



<p>Create a docker-compose file that will bring the Prometheus docker image up and running.</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 role="button" tabindex="0" data-code="services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
      - 9090:9090
    volumes:
      - ./prometheus/:/etc/prometheus/" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">services</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">prometheus</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">image</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prom/prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">container_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">command</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">&#39;--config.file=/etc/prometheus/prometheus.yml&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">ports</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">9090:9090</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">volumes</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./prometheus/:/etc/prometheus/</span></span></code></pre></div>



<p>the config file is mounted at the location <code>/etc/prometheus</code> and we use the location of the config file as an argument to the command.</p>



<h4 class="wp-block-heading"><strong>2.3 Test</strong></h4>



<p>1- Start spring application<br>2- Start the docker image with <code><em>docker compose up -d</em></code><br>3- Open the URL <a href="http://localhost:9090">http://localhost:9090</a> on browser.<br>4- Search for the label <code><em>http_server_requests_seconds_count</em></code></p>



<figure class="wp-block-image size-large has-custom-border is-style-default"><img decoding="async" width="1024" height="254" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/prometheus-01-1024x254.png" alt="" class="wp-image-69" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-1024x254.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-300x74.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-768x190.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-1536x380.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01.png 1910w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>In case you don&rsquo;t find the label, You can check if the job is running by navigating to <strong>« Status &gt; Targets »</strong>. You should see the state as “UP ».</p>



<figure class="wp-block-image size-large has-custom-border"><img loading="lazy" decoding="async" width="1024" height="257" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/prometheus-02-1024x257.png" alt="" class="wp-image-70" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-1024x257.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-300x75.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-768x193.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-1536x386.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02.png 1591w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, the data is getting ingested into Prometheus every 2 seconds.<br>Although Prometheus has a decent UI, Grafana&rsquo;s Dashboard is more powerful.</p>



<h3 class="wp-block-heading"><strong>3- Grafana</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Before starting Grafana we need to do some configuration to prepare the data source and dashboard. with that, we no longer need to create them manually.</p>



<h4 class="wp-block-heading"><strong>3.1 Datasource</strong></h4>



<p>First we need to add and configure the Prometheus data source.</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 role="button" tabindex="0" data-code="apiVersion: 1

# list of datasources that should be deleted from the database
deleteDatasources:
  - name: Prometheus
    orgId: 1

datasources:
- uid: prometheus
  orgId: 1
  name: Prometheus
  type: prometheus
  url: http://prometheus:9090 
  isDefault: true
  access: proxy
  readOnly: false
  editable: true" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">apiVersion</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># list of datasources that should be deleted from the database</span></span>
<span class="line"><span style="color: #569CD6">deleteDatasources</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  - </span><span style="color: #569CD6">name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">Prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">orgId</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">datasources</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">- </span><span style="color: #569CD6">uid</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">orgId</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">Prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">type</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">url</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">http://prometheus:9090</span><span style="color: #E6E6E6"> </span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">isDefault</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">access</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">proxy</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">readOnly</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">false</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">editable</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span></code></pre></div>



<h4 class="wp-block-heading"><strong>3.2 Dashboard</strong></h4>



<p>To prepare the Dashboard we need two things :</p>



<ul class="wp-block-list">
<li>Dashboard config file : dashboards.yaml</li>



<li>Dashboard source file : data-observability.json</li>
</ul>



<h4 class="wp-block-heading"><strong>3.3 Start service</strong></h4>



<p>Create a docker-compose file that will bring the Grafana docker image up and running.</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 role="button" tabindex="0" data-code="  grafana:
    image: grafana/grafana
    container_name: grafana
    depends_on:
      - prometheus
    ports:
      - 3000:3000
    user: root
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - ./grafana/datasources:/etc/grafana/provisioning/datasources # data sources
      - ./grafana/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml # dashboard setting
      - ./grafana/dashboards:/etc/grafana/dashboards # dashboard json files directory" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">grafana</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">image</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">grafana/grafana</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">container_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">grafana</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">depends_on</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">ports</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">3000:3000</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">user</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">root</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">environment</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">GF_SECURITY_ADMIN_USER=admin</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">GF_SECURITY_ADMIN_PASSWORD=admin</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">volumes</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/datasources:/etc/grafana/provisioning/datasources</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># data sources</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># dashboard setting</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/dashboards:/etc/grafana/dashboards</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># dashboard json files directory</span></span></code></pre></div>



<h4 class="wp-block-heading"><strong>3.4 Test</strong></h4>



<p>2- Start the docker image with <code>docker compose up -d</code><br>3- Open the URL http://localhost:3000 on browser.</p>



<figure class="wp-block-image size-large has-custom-border"><img loading="lazy" decoding="async" width="1024" height="453" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/grafana-01-1024x453.png" alt="" class="wp-image-67" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-1024x453.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-300x133.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-768x340.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01.png 1236w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>We can see our Dashboard that we have already configured</p>



<figure class="wp-block-image size-large has-custom-border"><img loading="lazy" decoding="async" width="1024" height="497" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/grafana-02-1024x497.png" alt="" class="wp-image-68" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-1024x497.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-300x146.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-768x373.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-1536x745.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02.png 1843w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading"><strong>4. Source code</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>The complete source code of this article can be found <a href="https://github.com/JKaouech/spring-boot/tree/master/spring-prometheus">over on GitHub.</a></p>



<h3 class="wp-block-heading"><strong>Reference</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p><a href="https://refactorfirst.com/spring-boot-prometheus-grafana">https://refactorfirst.com/spring-boot-prometheus-grafana</a></p>



<p><a href="https://dev.to/luafanti/spring-boot-monitoring-with-prometheus-operator-40g1">https://dev.to/luafanti/spring-boot-monitoring-with-prometheus-operator-40g1</a></p>



<p><a href="https://grafana.com/blog/2022/04/26/set-up-and-observe-a-spring-boot-application-with-grafana-cloud-prometheus-and-opentelemetry/">https://grafana.com/blog/2022/04/26/set-up-and-observe-a-spring-boot-application-with-grafana-cloud-prometheus-and-opentelemetry/</a></p>



<p><a href="https://betterprogramming.pub/how-to-monitor-a-spring-boot-app-with-prometheus-and-grafana-22e2338f97fc">https://betterprogramming.pub/how-to-monitor-a-spring-boot-app-with-prometheus-and-grafana-22e2338f97fc</a></p>
<p>L’article <a href="https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/">Monitoring with Prometheus and Grafana</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Spring boot actuator</title>
		<link>https://www.ji-ka.tn/spring-boot-actuator/</link>
					<comments>https://www.ji-ka.tn/spring-boot-actuator/#respond</comments>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Sat, 30 Sep 2023 09:20:12 +0000</pubDate>
				<category><![CDATA[spring boot]]></category>
		<category><![CDATA[actuator]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=19</guid>

					<description><![CDATA[<p>In this article, we introduce the Spring Boot Actuator, take a look at how to customize the Spring Boot Actuators’&#160;/info&#160;endpoint and &#160;how to track and monitor this startup information. 1 Actuator Actuator endpoints let you monitor and interact with your Spring Boot application. 1.1 Setup By adding the following dependency to your project:&#160; the actuator [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/spring-boot-actuator/">Spring boot actuator</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we introduce the Spring Boot Actuator, take a look at how to customize the Spring Boot Actuators’&nbsp;<em>/info</em>&nbsp;endpoint and &nbsp;how to track and monitor this startup information.</p>



<span id="more-19"></span>



<h3 class="wp-block-heading" id="user-content-1--actuator"><strong>1 Actuator</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Actuator endpoints let you monitor and interact with your Spring Boot application.</p>



<h4 class="wp-block-heading" id="user-content-11-setup"><strong>1.1 Setup</strong></h4>



<p>By adding the following dependency to your project:&nbsp;</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 role="button" tabindex="0" data-code="&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependencies</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-actuator</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependencies</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p>the actuator endpoints will become available in your project. :&nbsp;<a href="http://localhost:8080/actuator">http://localhost:8080/actuator</a></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Be aware that all actuators except for&nbsp;<code>/health</code>&nbsp;are disabled by default for security reasons. They might expose sensitive information. You can use the&nbsp;<code>management.endpoints.web.exposure.include</code>&nbsp;property to enable the actuators.</p>
</blockquote>



<p>To expose all actuator endpoint : </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 role="button" tabindex="0" data-code="management:
  endpoints:
    web:
      exposure:
        include: '*'" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoints</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">web</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">exposure</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">include</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;*&#39;</span></span></code></pre></div>



<p>**To expose only info actuator endpoint : </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 role="button" tabindex="0" data-code="management:
  endpoints:
    web:
      exposure:
        include: &quot;info&quot;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoints</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">web</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">exposure</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">include</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;info&quot;</span></span></code></pre></div>



<p>You can reach the info actuator on your local machine:&nbsp;<a href="http://localhost:8080/actuator/info">http://localhost:8080/actuator/info</a>&nbsp;once you start your Spring Boot application.</p>



<h4 class="wp-block-heading" id="user-content-12-spring-boot-info-actuator-contributors"><strong>1.2 Spring Boot info actuator contributors</strong></h4>



<p>There are different info contributors that “contribute” to the information exposed by the actuator info endpoint:</p>



<ul class="wp-block-list">
<li><strong>build</strong>&nbsp;Enabled by default:&nbsp;<code>true</code>&nbsp;Goal: Exposes build information. Requires: you to generate build information Configuration property:&nbsp;<code>management.info.build.enabled</code></li>



<li><strong>env</strong>&nbsp;Enabled by default:&nbsp;<code>false</code>&nbsp;(since Spring Boot 2.6. For the older Spring Boot version, this contributor is enabled by default!) Goal: Exposes any property from the SpringEnvironment . Required: to specify properties with names that start with info.* in you application.yml or application.properties Configuration property:&nbsp;<code>management.info.env.enabled</code></li>



<li><strong>git</strong>&nbsp;Enabled by default:&nbsp;<code>true</code>&nbsp;Goal: exposes git information. Requires: you to generate&nbsp;<a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.build.generate-git-info">git information</a>&nbsp;Configuration property:&nbsp;<code>management.info.git.enabled</code></li>



<li><strong>java</strong>&nbsp;Enabled by default:&nbsp;<code>false</code>&nbsp;(since Spring Boot 2.6) Goal: exposes Java runtime information. Configuration property:&nbsp;<code>management.info.java.enabled</code></li>
</ul>



<h4 class="wp-block-heading" id="static-properties-in-info"><strong>1.3 Static Properties in /info</strong></h4>



<p>You can customize the environment info contributor by adding&nbsp;<code>info.*</code>properties to your application.yml or application.properties :</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 role="button" tabindex="0" data-code="management.info:
  env.enabled: true
info:
  application:
    name: ${spring.application.name}
    description: spring actuator application
    version: '@project.version@'" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management.info</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">env.enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span>
<span class="line"><span style="color: #569CD6">info</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">application</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${spring.application.name}</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">description</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">spring actuator application</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">version</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;@project.version@&#39;</span></span></code></pre></div>



<h4 class="wp-block-heading" id="git-details-in-info"><strong>1.4 GIT details in /info</strong></h4>



<p>We can add git details using the respective Maven 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 role="button" tabindex="0" data-code="&lt;dependency&gt;
    &lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
    &lt;artifactId&gt;git-commit-id-plugin&lt;/artifactId&gt;
&lt;/dependency&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">pl.project13.maven</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">git-commit-id-plugin</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p>and enable management.info.git in configuration property:</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 role="button" tabindex="0" data-code="management.info:
 git.enabled: true" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management.info</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6"> </span><span style="color: #569CD6">git.enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span></code></pre></div>



<h4 class="wp-block-heading" id="git-build-information-details-in-info"><strong>1.5 GIT build information details in /info</strong></h4>



<p>We can also include build information including name, group, and version using the Maven plugin:</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 role="button" tabindex="0" data-code="<plugin&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-maven-plugin</artifactId&gt;
    <executions&gt;
        <execution&gt;
            <goals&gt;
                <goal&gt;build-info</goal&gt;
            </goals&gt;
        </execution&gt;
    </executions&gt;
</plugin&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">plugin</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-maven-plugin</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">executions</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">execution</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">goals</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">                </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">goal</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">build-info</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">goal</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">goals</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">execution</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">executions</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">plugin</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p>and enable management.info.build in configuration property:</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 role="button" tabindex="0" data-code="management.info:
 build.enabled: true" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management.info</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6"> </span><span style="color: #569CD6">build.enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span></code></pre></div>



<h3 class="wp-block-heading" id="startup-tracking"><strong>2 Startup Tracking</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Tracking the various steps during application startup can provide useful information that can help us to understand the time spent during various phases of application startup. Such instrumentation can also improve our understanding of the context lifecycle and the application startup sequence.</p>



<h4 class="wp-block-heading" id="setup-1"><strong>2.1 Setup</strong></h4>



<ul class="wp-block-list">
<li>Add the spring-boot-starter-actuator dependency to our POM</li>
</ul>



<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 role="button" tabindex="0" data-code="<dependencies&gt;
  <dependency&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-starter-web</artifactId&gt;
  </dependency&gt;
  <dependency&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-starter-actuator</artifactId&gt;
  </dependency&gt;
</dependencies&gt;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependencies</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-actuator</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependencies</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Expose the required endpoint over HTTP by setting the configuration property in our application properties file:</li>
</ul>



<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 role="button" tabindex="0" data-code="management:
  endpoints:
    web:
      exposure:
        include: &quot;startup&quot;" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">management</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoints</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">web</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">exposure</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">include</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;startup&quot;</span></span></code></pre></div>



<h4 class="wp-block-heading" id="startup-endpoint"><strong>2.2 Startup Endpoint</strong></h4>



<p>We need to set the application&rsquo;s startup configuration to an instance of `BufferingApplicationStartup`. This is an in-memory implementation of the `ApplicationStartup` interface provided by Spring Boot. It captures the events during Spring&rsquo;s startup process and stores them in an internal buffer.</p>



<ul class="wp-block-list">
<li>Create a simple application with this implementation:</li>
</ul>



<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 role="button" tabindex="0" data-code="@SpringBootApplication
public class SpringActuatorApplication {
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringActuatorApplication.class);
    app.setApplicationStartup(new BufferingApplicationStartup(2048));
    app.run(args);
  }
}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">SpringBootApplication</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">SpringActuatorApplication</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">static</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">void</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">main</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6">[] </span><span style="color: #9CDCFE">args</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #4EC9B0">SpringApplication</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">SpringApplication</span><span style="color: #E6E6E6">(</span><span style="color: #9CDCFE">SpringActuatorApplication</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">class</span><span style="color: #E6E6E6">);</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">setApplicationStartup</span><span style="color: #E6E6E6">(</span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">BufferingApplicationStartup</span><span style="color: #E6E6E6">(</span><span style="color: #B5CEA8">2048</span><span style="color: #E6E6E6">));</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">run</span><span style="color: #E6E6E6">(args);</span></span>
<span class="line"><span style="color: #E6E6E6">  }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Now, we can start our application and query the startup actuator endpoint.&nbsp;<a href="http://localhost:8080/actuator/startup">http://localhost:8080/actuator/startup</a></li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Be aware before spring boot version 2.6 startup endpoint only allow POST method call.</p>
</blockquote>



<h4 class="wp-block-heading" id="filtering-startup-events"><strong>2.3 Filtering Startup Events</strong></h4>



<p>The buffering implementation has a fixed capacity for storing events in memory. Therefore, it might not be desirable to store a large number of events in the buffer.</p>



<p>We can filter the instrumented events and only store those that may be of interest to us:</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 role="button" tabindex="0" data-code="	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(SpringActuatorApplication.class);
		BufferingApplicationStartup startup = new BufferingApplicationStartup(2048);
		startup.addFilter(startupStep -&gt; startupStep.getName().matches(&quot;spring.boot.application.starting&quot;));
		app.setApplicationStartup(startup);
		app.run(args);
	}" style="color:#E6E6E6;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="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">static</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">void</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">main</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6">[] args) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">SpringApplication</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">SpringApplication</span><span style="color: #E6E6E6">(</span><span style="color: #9CDCFE">SpringActuatorApplication</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">class</span><span style="color: #E6E6E6">);</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">BufferingApplicationStartup</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">startup</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">BufferingApplicationStartup</span><span style="color: #E6E6E6">(</span><span style="color: #B5CEA8">2048</span><span style="color: #E6E6E6">);</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #9CDCFE">startup</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">addFilter</span><span style="color: #E6E6E6">(startupStep </span><span style="color: #569CD6">-&gt;</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">startupStep</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getName</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">matches</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;spring.boot.application.starting&quot;</span><span style="color: #E6E6E6">));</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">setApplicationStartup</span><span style="color: #E6E6E6">(startup);</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #9CDCFE">app</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">run</span><span style="color: #E6E6E6">(args);</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span></code></pre></div>



<p>Here, we&rsquo;ve used the addFilter method to only instrument steps with the specified name.</p>



<h3 class="wp-block-heading" id="user-content-4-source-code"><strong>3 Source code</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>The complete source code of this article can be found&nbsp;<a href="https://github.com/JKaouech/spring-boot/tree/master/spring-actuator">over on GitHub.</a></p>



<h3 class="wp-block-heading" id="user-content-reference"><strong>Reference</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p><a href="https://www.baeldung.com/spring-boot-actuators">https://www.baeldung.com/spring-boot-actuators</a></p>



<p><a href="https://www.baeldung.com/spring-boot-info-actuator-custom">https://www.baeldung.com/spring-boot-info-actuator-custom</a></p>



<p><a href="https://medium.com/@TimvanBaarsen/help-my-spring-boot-info-actuator-endpoint-is-enabled-but-i-dont-see-any-environment-details-c2d41a7b24d7">https://medium.com/@TimvanBaarsen/help-my-spring-boot-info-actuator-endpoint-is-enabled-but-i-dont-see-any-environment-details-c2d41a7b24d7</a></p>



<p><a href="https://www.baeldung.com/spring-boot-actuator-startup">https://www.baeldung.com/spring-boot-actuator-startup</a></p>



<p><a href="https://medium.com/techwasti/startup-actuator-endpoint-spring-boot-54a8dd0a1fdb">https://medium.com/techwasti/startup-actuator-endpoint-spring-boot-54a8dd0a1fdb</a></p>
<p>L’article <a href="https://www.ji-ka.tn/spring-boot-actuator/">Spring boot actuator</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ji-ka.tn/spring-boot-actuator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
