<?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>Web Development &#8211; jasta</title>
	<atom:link href="https://jasta.io/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>https://jasta.io</link>
	<description>Beyond Syntax</description>
	<lastBuildDate>Sat, 06 Apr 2024 17:27:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://jasta.io/wp-content/uploads/2024/01/cropped-jasta_favicon-32x32.webp</url>
	<title>Web Development &#8211; jasta</title>
	<link>https://jasta.io</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Create a powerful Java Web Application with Spring</title>
		<link>https://jasta.io/web-development/create-a-java-web-application-with-spring/</link>
					<comments>https://jasta.io/web-development/create-a-java-web-application-with-spring/#respond</comments>
		
		<dc:creator><![CDATA[Jakob]]></dc:creator>
		<pubDate>Sat, 24 Feb 2024 10:54:32 +0000</pubDate>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Web App]]></category>
		<guid isPermaLink="false">https://jasta.io/?p=2668</guid>

					<description><![CDATA[Hello tech enthusiasts! Welcome back to jasta, your go-to destination for all things tech. Today, we’re diving into&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Hello tech enthusiasts! Welcome back to jasta, your go-to destination for all things tech. Today, we’re diving into how to create a powerful Java Web Application with Spring, providing a step-by-step guide to help you accomplish this on your computer. Whether you’re a seasoned tech guru or just starting your digital journey, our straightforward instructions will make the process a breeze. Let’s jump in and create a powerful Java Application with Spring.</p>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#create-your-project-with-spring-initializr">Create your Project with Spring Initializr</a></li><li><a href="#import-your-java-web-application-with-spring-into-intelli-j-idea">Import your Java Web Application with Spring into IntelliJ IDEA</a><ul><li><a href="#open-a-project-from-the-starting-screen">Open a Project from the starting screen</a></li><li><a href="#open-a-project-inside-intelli-j-idea">Open a Project inside IntelliJ IDEA</a></li></ul></li><li><a href="#adding-rest-methods-to-your-java-web-application-with-spring">Adding REST Methods to your Java Web Application with Spring</a></li><li><a href="#try-out-your-java-web-application">Try out your Java Web Application</a></li></ul></nav></div>



<h2 class="wp-block-heading" id="create-your-project-with-spring-initializr">Create your Project with Spring Initializr</h2>



<p>To create a Java web application with Spring you first have to create the project via the <a href="https://start.spring.io/" target="_blank" rel="noopener">Spring Initializr</a>. There you have a quit similar screen to the IntelliJ IDEA project creation window. </p>



<p>First you have to set the build tool with a corresponding language. As in the Java tutorials you should pick Gradle with Kotlin, since Gradle and Kotlin are supiror to Maven and Groovy. After choosing the build tool you have to choose the language for the Project, which in this case is Java. Below you can select the wanted Spring Boot version. It is recommended to just use the most revent final version which in this case is 3.2.3.</p>



<p>After setting the build tool, language and Spring Boot version you now have to enter some information about your Java web application with Spring. You can change these as you like, but you can also just leave all the prefilled values or just give the project another name.</p>



<p>Below these settings you can set the packaging and Java version. For the packaging I would recommend to just use the already selected option, Jar. For the Java version it is the best to pick the newest one. In this case this is 21.</p>



<p>On the right side of the Spring Initializr you can see the dependencies for the project. Because we are later adding some REST methods, we are going to add a certain dependency. For that you have to click on ADD DEPENDENCIES or press CTRL + B to open the dependencies window. There you have to select &#8216;Spring Web&#8217; which is also marked in the screenshot below.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img fetchpriority="high" decoding="async" width="759" height="1024" src="https://jasta.io/wp-content/uploads/2024/02/SpringDependencies-759x1024.webp" alt="Spring Initializr dependencies" class="wp-image-2692" style="width:644px" srcset="https://jasta.io/wp-content/uploads/2024/02/SpringDependencies-759x1024.webp 759w, https://jasta.io/wp-content/uploads/2024/02/SpringDependencies-222x300.webp 222w, https://jasta.io/wp-content/uploads/2024/02/SpringDependencies-768x1036.webp 768w, https://jasta.io/wp-content/uploads/2024/02/SpringDependencies-512x691.webp 512w, https://jasta.io/wp-content/uploads/2024/02/SpringDependencies.webp 800w" sizes="(max-width: 759px) 100vw, 759px" /></figure>
</div>


<p>Now after filling out all necessary information to create the Java web application with Spring, your Screen should look similar to the screenshot below. If this is the case, you can now click on GENERATE button on the bottom or start generating with CTRL + ENTER to generate the project and download it automatically.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="748" src="https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-1024x748.webp" alt="Java Web Application with Spring" class="wp-image-2691" srcset="https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-1024x748.webp 1024w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-300x219.webp 300w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-768x561.webp 768w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-1536x1123.webp 1536w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-512x374.webp 512w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings-920x672.webp 920w, https://jasta.io/wp-content/uploads/2024/02/SpringInitializrAllSettings.webp 1587w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h2 class="wp-block-heading" id="import-your-java-web-application-with-spring-into-intelli-j-idea">Import your Java Web Application with Spring into IntelliJ IDEA</h2>



<p>After generating and downloading your project, you now have a zip file inside your Downloads folder. If this is the case, you should now relocate the zip file to a folder where you want to locate the project and unzip it there. After unzipping it, you can now open IntelliJ IDEA.</p>



<p>There are two different methods to open your Java Web application with Spring depending if you have already opened a project or not.</p>



<h3 class="wp-block-heading" id="open-a-project-from-the-starting-screen">Open a Project from the starting screen</h3>



<p>If you currently don&#8217;t have an open project inside IntelliJ you should see the following screen when starting the program up.  If this is the case, you have to click on open and find your project. Inside your project folder you have to select your build.gradle.kts file and click on the OK button.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" width="778" height="636" src="https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJ.webp" alt="Open project" class="wp-image-2681" style="width:644px" srcset="https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJ.webp 778w, https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJ-300x245.webp 300w, https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJ-768x628.webp 768w, https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJ-512x419.webp 512w" sizes="(max-width: 778px) 100vw, 778px" /></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" width="417" height="485" src="https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJBuildGradle.webp" alt="Select build.gradle" class="wp-image-2682" style="width:344px" srcset="https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJBuildGradle.webp 417w, https://jasta.io/wp-content/uploads/2024/02/OpenProjectIntelliJBuildGradle-258x300.webp 258w" sizes="auto, (max-width: 417px) 100vw, 417px" /></figure>
</div>


<p>After clicking on OK you will see the dialog from the screenshot below on your screen, because IntelliJ is understanding that the build.gradle.kts file belongs to a project and now asks you if you want to open the whole project or just the file. So you have to click on Open as Project.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="480" height="138" src="https://jasta.io/wp-content/uploads/2024/02/OpenAsProjectIntelliJ.webp" alt="Open as Project" class="wp-image-2683" srcset="https://jasta.io/wp-content/uploads/2024/02/OpenAsProjectIntelliJ.webp 480w, https://jasta.io/wp-content/uploads/2024/02/OpenAsProjectIntelliJ-300x86.webp 300w" sizes="auto, (max-width: 480px) 100vw, 480px" /></figure>
</div>


<h3 class="wp-block-heading" id="open-a-project-inside-intelli-j-idea">Open a Project inside IntelliJ IDEA</h3>



<p>If you have already opened a Project in IntelliJ you firstly have to click on File on the top navigation bar, select new and then click on &#8216;Project from Existing Sources&#8230;&#8217;. You then have to find your unzipped project and select the build.gradle.kts file inside the project folder.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" width="643" height="578" src="https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntellij.webp" alt="Project from Existing Sources inside IntelliJ" class="wp-image-2684" style="width:644px" srcset="https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntellij.webp 643w, https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntellij-300x270.webp 300w, https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntellij-512x460.webp 512w" sizes="auto, (max-width: 643px) 100vw, 643px" /></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" decoding="async" width="422" height="570" src="https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntelliJBuildGradle.webp" alt="Selecting project file (build.gradle)" class="wp-image-2685" style="width:344px" srcset="https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntelliJBuildGradle.webp 422w, https://jasta.io/wp-content/uploads/2024/02/ProjectFromExistingSourceIntelliJBuildGradle-222x300.webp 222w" sizes="auto, (max-width: 422px) 100vw, 422px" /></figure>
</div>


<p>Wheter you have already opened a Project or not you will probably see the warning message from the screenshot below on your computer. Since you created that project by yourself on an official website of Spring you can just select Trust Project. If you don&#8217;t want to see such warning messages in the future again you can also set the checkbox above the buttons to trust the whole parent folder of the just opened project.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="509" height="222" src="https://jasta.io/wp-content/uploads/2024/02/IntelliJTrustProject.webp" alt="IntelliJ trust project" class="wp-image-2686" srcset="https://jasta.io/wp-content/uploads/2024/02/IntelliJTrustProject.webp 509w, https://jasta.io/wp-content/uploads/2024/02/IntelliJTrustProject-300x131.webp 300w" sizes="auto, (max-width: 509px) 100vw, 509px" /></figure>
</div>


<p>After all those steps your project structure on the left looks like that.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="397" height="491" src="https://jasta.io/wp-content/uploads/2024/02/ImportedSpringProject.webp" alt="Final Project structure" class="wp-image-2688" srcset="https://jasta.io/wp-content/uploads/2024/02/ImportedSpringProject.webp 397w, https://jasta.io/wp-content/uploads/2024/02/ImportedSpringProject-243x300.webp 243w" sizes="auto, (max-width: 397px) 100vw, 397px" /></figure>
</div>


<h2 class="wp-block-heading" id="adding-rest-methods-to-your-java-web-application-with-spring">Adding REST Methods to your Java Web Application with Spring</h2>



<p>Since your just imported demo project will do nothing, we are going to add two REST Methods to your project and try them out later on. </p>



<p>First of all you have to add a new class inside your project. How to do that is already covered in <a href="https://jasta.io/java-basics/create-your-first-java-program-with-ease/">this</a> article. You can choose whatever name you like. For this example, I am naming it &#8216;DemoController&#8217;. To tell the Java web application with Spring that this class will carry REST methods, you have to add the @RestController annotation like below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.example.demo;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
}</pre>



<p>Now we are going to add two different REST methods to your DemoController. The first method is to just greet you. This can be done with the following code.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    
    @GetMapping("/greet/{name}")
    public String greetMe(@PathVariable String name) {
        return "Hello " + name + "!";
    }
}</pre>



<p>The @GetMapping annotation tells the program that this is a GET method and can be accessed when you add /greet to your web address. With the {name} inside the @GetMapping the program knows, that it should use everything after the /greet/ as the method parameter which is annotated with @PathVariable. It is important, that the method parameter and the variable name inside the curly brackets are the same.</p>



<p>The second method we are going to add will sum up two numbers. This is archived with adding the sumOfTwoNumbers method like below.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.example.demo;

import org.springframework.web.bind.annotation.*;

@RestController
public class DemoController {

    @GetMapping("/greet/{name}")
    public String greetMe(@PathVariable String name) {
        return "Hello " + name + "!";
    }

    @GetMapping("/sum")
    public String sumOfTwoNumbers(@RequestParam("num1") int number1, @RequestParam("num2") int number2) {
        return "Sum: " + (number1 + number2);
    }
}</pre>



<p>As above the @GetMapping is telling us where to find the method. But now we don&#8217;t want to use PathVariables like in the greetMe method but rather use @RequestParam instead so the parameters are now not getting filled with the value behind /sum/. Instead of that we are now interested in parameters like the following: /sum?num1=5&amp;num2=10 .</p>



<h2 class="wp-block-heading" id="try-out-your-java-web-application">Try out your Java Web Application</h2>



<p>To try out your just-added REST methods, you have to go back to the DemoApplication class and click on the play button next to your code. You should then see the bootup process in your build output window at the bottom. After you see Completed initialization in the build window you can test your methods.</p>



<p>To test the greet method you simply have to open your browser and type http://localhost:8080/ to get to your web application and then add /greet/&lt;YourName&gt; to it so your address bar should look like this: http://localhost:8080/greet/&lt;YourName&gt;. If you then hit enter you see &#8216;Hello &lt;YourName&gt;!&#8217;.</p>



<p>If that was working fine, you can now try the second method. For that you have to remove the /greet/&lt;YourName&gt; part of the url so you just have http://localhost:8080/ again and then add /sum with two request parameters named num1 and num2. So the final request should look like this: http://localhost:8080/sum?num1=&lt;Number1&gt;&amp;num2=&lt;Number2&gt;. After clicking enter again you can now see the sum of the two numbers you entered.</p>



<p>Assuming everything is working fine, you have successfully created a Java web application with Spring. If you have any troubles feel free to leave a comment.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://jasta.io/web-development/create-a-java-web-application-with-spring/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
