1115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
Marcos Maia
Posted on Jun 30, 2019 + Updated on Jul 4, 2019
Spring Boot - Crash course
ispringboot #java tutorial #beginners
The goal of this post is to enable you to run your first Spring Boot Application as most of
my posts here will require you to start from there.
Spring _boot is a framework that applies a lot of opinionated configurations by default,
which makes it a very simple framework to start developing within the Java ecosystem, it
also has great support for testing, good support for Kotlin although | would recommend
caution and would wait before using heavily on the server side to run Kotlin projects in
production, for Android development its another story, Kotlin is currently the
recommended language to use.
With Spring Boot no XML configuration is needed, which makes it very easy to create and
run Spring/Java applications, the downside is that you need to understand it's
conventions to know exactly what's going on, especially when troubleshooting issues.
It's really great that it also provides many nice features required in real projects including
non-functionals which is very useful and time saving.
To create a Spring Boot application the easiest way is using a plugin integrated with your
hitps:dev tanthogrootspring boot erash-course-2tnm ve1115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
favorite IDE or go to the Spring Boot Initializer website and create your initial application
setup there. It's quite easy to use and offers an enormous amount of possible
combinations from the Spring ecosystem.
Let's create our first Spring Boot Application and run it, if you don't have java 8 or java 11
installed check out my previous post where you will find directions on all you'll need to.
set up in order to run the sample projects from my blog posts, including this one. Now,
open the Spring Initializr website and follow the steps below:
1. Project: Maven
2. Language: Java
3. Spring Boot: Pick the latest release currently it's 2.1.6
4, Project Metadata
* Group: io.stockgeeks
* artifact: firstapp
* Options
= Name: firstapp
= Description: My first project with Spring Boot
= Package Name: io.stockgeeks firstapp
= Packaging: Jar
= Java: 11
5. Dependencies: Search and add spring Web starter, make sure it's under Selected
dependencies.
Click Generate the Project big green button and download the zip file, save it to your
preferred location, Extract the zip file with your preferred compression tool.
hitps:dev lanthogroatspring boot erash-course-2tnm 281115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
Spring Initialize
Langunee eve
ya ict ith Spring Bot
lostockgeeks nap
Now let's run it. Make sure you have Java 11 and maven installed.
Navigate to the folder where you extracted the zip file and check the files in there, you
should have a pon.xn1 file which is the maven parent pom for your project. If you're using
an IDE you can now load this folder in it and it should recognize it as a maven project, but
for now, we're going to run the application using the command line.
Download the dependencies, compile it and generate an executable jar file using maven:
mvn clean package
You should see a BUILD SUCCESS message like the screenshot below:
hitps:dev lanthogroatspring boot erash-course-2tnm ae1115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
Now you'll have a target folder created, this folder is completely generated during the
build process and you can remove it at any time, Make sure to have it added to your
-gitignore file as you don't want to commit this folder's contents at all, this folder is
where you will also find all compiled and packaged structure generated by the build and
packaging process.
If you list the contents of the target folder you should see a file with the name firstapp-
0.0.1-SNAPSHOT. jar, this is the file containing your executable application, it has an
embedded java enabled , which by default is but you can also
using maven and spring boot plugin:
vn spring-boot:run
or using java to run the generated jar file:
java -jar target/[email protected].
Once you see the message: Tomcat started on port(s): 8@8@ (http) you have your Spring
Boot Application Running.
To finish with something more useful let's add a REST endpoint and a static HTML page.
hitps:dev lanthogrootspring boot erash-course-2tnm 481115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
Add an endpoint and default static page
index.html
Create a folder named public and add an index.html page with any content you want
under /src/main/resources/public.
Spring Boot automatically add the public folder content to be served, it also by default
enables other paths under resources:
/META-INF/resources/
/resources/
/static/
/public/
Rest Endpoint
Let's now create the simplest possible REST endpoint, create a java class on the same
package level where you have the FirstappAplication java definition, in my case package
io.stockgeeks firstapp , call it: HeLlospringrest. java
Add the following content:
package ‘
import, 3
import 5
import 5
@controller
G@RequestMapping("/api")
hitps:dev afthogroolspring boot erash-course-2tnm se1115725, 3.09 PM Spring Bot Crash course - DEV Community FEY
public class HelloSpringRest {
@GetMapping("/hello")
public String hello(@pathParam("name”) String name) {
return “Hello, " + names
}
}
Allit takes to create REST endpoints with spring is some annotations and few lines of java
code.
The @controller annotation sinalizes to Spring Boot that this is a spring managed
component, the @equestMapping(“api") prepares the REST endpoint root context for
endpoints added to this class and map them as "/api", the @GetMapping("/hello")
registers the context "/hello" to receive HTTP GET requests and finally the
@PathParam(“name’) registers a query string for the GET call with the key "name".
Build and run
As before anytime you want to run from the command line you can build and run: va
clean package && mvn spring-boot:run..
Now navigate to: http://localhost:8080 and you should see the static index.htm! page that
was just created.
To test the REST endpoint you can point the browser to http://locahost:8080/api/hello?
name=Spock or use curl: curt -x GET http: //locahost:8080/api /nello?nane=spock
Done
You have created a Spring Boot Application and a static page and REST endpoint. The
goal of this post was to enable you to easily create Spring Boot Applications and | will
refer back to this in my future posts because most of the posts here will require having a
Spring Boot Application as a starting point.
If you want to see the source code and didn't follow the aforementioned steps you can
clone this post project repo with: git clone [email protected]:stockgeeks/firstapp.git
References
Can = at vesithy Cn.
hitps:dev lafthogroatspring boot-crash-course-2tnm oe1115723, 3.09 PM
DELVINY SlaUe CUrLeNe Witt 2p
1y_DULL
Spring Boot Rest Guide
Header Photo by wu yi on Unsplash
Top comments (0)
Code of Conduct
Here is a post you might want to check out
Regex for lazy developers
Soring Boot- Crash course -DEV Community PRE
Report abuse
fr RR
T KNOW
PRESSIONS
BR
~
Soa
A
Sorry for the callout @
Ji
Rl a K
e Marcos M:
|am an experienced Developer, Trainer, and eventu
Iipsidevlathegroolpring-boot-crash-course-2tnm
al Speaker. Lifelong learner, eager to learn and
781115123, 3.09 PM Spring Boat Crash course - DEV Community FEY
share knowledge. A bit introspective. Keep coding. Be humble. Help otners,
LOCATION
Amsterdam
WORK
Staff Engineer @ Bitvavo
JOINED
May 28, 2019
More from Marcos Maia
Spring Katka Streams playground with Kotlin - V
¥kafka #kotlin #springboot ‘tutorial
Spring Kafka Streams playground with Ketlin - IV
‘#kotlin #katka #springboot #tutorial
Spring Kafka Streams playground with Kotlin - Ill
Ykotlin #katka “springboot ‘tutorial
hitpsidevtonnegrooispring boot-crash-course-2inm
ae