Hello, Spring Boot fans!
Steve Perry here.
This video accompanies the IBM developerWorks
CAPTION: Spring Boot Basics http://www.ibm.com/developerworks/library/j-spring-boot-basics-perry/index.html
If you've been following along, then you already have an idea of what Spring Boot does, and
have seen a simple use with the HelloSpringBoot application.
In this video, I want to show you a more slightly complicated use of Spring Boot.
I've written a Spring-based POJO application that manages a simple TODO list.
Around that application I've written a Spring Boot wrapper.
In this video I'd like to do 5 things:
* Get the source code from GitHub
* Import the code into Eclipse, and tour the code
* Build the projects, including the executable JAR
* Run the executable JAR
* And exercise the application using SoapUI
Let's get started.
**********************************************
CAPTION: 1.
Clone the GitHub repos
First, I'll clone the code from GitHub.
There are two projects, and I'll pull them both.
I need to create a directory on my computer where I'll
clone the repos, so I'll do that now.
$ mkdir /Users/sperry/home/SpringBoot
I'll navigate to that folder, and pull both repos.
First, the odotCore repo, which contains the bulk of
the code for the simple TODO application.
git clone https://github.com/makotogo/odotCore
Then, I'll clone the repo for the SpringBootDemo wrapper.
git clone https://github.com/makotogo/SpringBootDemo
*****
CAPTION: 2.
Import code into Eclipse
Now that I have the code, I'll import it into Eclipse.
Go to File, Import, Maven, Existing Maven projects,
click Next, and use the browse button to locate the directory where
I cloned the projects, or if you know the path, you can type
it here (INDICATE ROOT DIRECTORY TEXT FIELD).
Select both projects, and click Finish.
CAPTION: Tour the code (briefly)
I don't have enough time in this video to do a complete
tour of the code, but I would like to show you enough
just to get you oriented.
The first class I'll show you is in the odotCore project,
and is called CategoryService.java.
This is a Spring @Service class that uses a @Bean
called CategoryDao for doing Database access that is
@Autowired through Spring.
It has several methods, which by and large simply
delegate to CategoryDao (bean).
Next is the ItemService class, which follows the
same idiom as the CategoryService.
Next up is the CategoryDao, which is a Spring @Component and @Autowired with a DataSource
bean.
The methods on CategoryDao use embedded SQL, with
substitution parameters, and Spring's JdbcTemplate class to do its work.
As with ItemService, ItemDao follows the same pattern
as CategoryDao.
Now I want to show you some of the code in SpringBootDemo.
First is the App class, which is the driver for
the SpringBootDemo application.
It's annotated with the @SpringBootApplication annotation, its main()
method delegates to SpringApplication.run().
We also define a @Bean called commandLineRunner that
is wired up through Spring and executed when the app
starts up.
Next is the SpringBootDemoConfiguration, which extends the odotCore class AbstractApplicationConfiguration
and must implement the getDataSource() method, where
it creates the DataSource @Bean used to access the DB.
It uses a Derby embedded database which exists only for
the life of the JVM.
When the DB starts, it executes two scripts: one to create the tables, and
the other to insert data into the DB.
Later you may be wondering where the data comes from, and the answer
is the insert_data.sql script executed here.
Then there is the @RestController called SpringBootDemoController.
It defines two @Beans that wrap the @Service classes in
odotCore.
These are accessed and delegated to by CategoryRestService and ItemRestService.
These classes provide the Spring MVC REST method
implementations.
*****
CAPTION: 3.
Build projects
Now I'll build both projects from Eclipse, and from
the command line.
Both use Maven, and both are fine to use.
But since this is a tutorial, I want to show both.
First, I'll build from within Eclipse.
I right-click on the odotCore project and setup my
Maven Run configuration, and use the clean and
install goals.
I choose install because I want the odotCore JAR
file in my local Maven repository when the build
is finished.
The unit tests take a minute or so to run, so
I'll speed these up.
CAPTION: Video 2x speed
The BUILD SUCCESS message from Maven tells me the
build ran successfully.
Now, I'll build the SpringBootDemo wrapper project.
For its goals, I'll choose clean and package.
And from the BUILD SUCCESS message, I know the build
worked.
To build from the command line, I'll open a
terminal window on my Mac.
First, I'll navigate to the odotCore project, and the odotCore directory, then build the
odotCore project:
mvn clean install
CAPTION: Video 3x speed
And from the BUILD SUCCESS message, I know the build
worked.
Next, I'll navigate to the SpringBootDemo directory,
then build the SpringBootDemo wrapper:
mvn clean package
Again, from the BUILD SUCCESS message, I know the build
worked.
*****
CAPTION: 4.
Run executable JAR
To run the executable JAR, navigate to the SpringBootDemo directory, and then run
java -jar target/SpringBootDemo-1.0-SNAPSHOT.jar
And watch SpringBoot come alive.
When I see the phrase "Started App" I know the
app is up and running.
Notice Spring Boot tells us the paths to which it
has bound the REST methods, telling us that the app
is up and functioning.
*****
CAPTION: 5.
Exercise the application
The first thing I want to do is run a quick smoke
test on the application.
To do that I'll open a browser, and hit the URL:
http://localhost:8080/CategoryRestService/FindAll
What I see is a JSON response containing all of the
Category objects in the Apache Derby database that
I have configured to work with the SpringBootDemo application.
When I see these objects, I know it's working.
The SpringBootDemo application has 12 RESTful methods
and I don't have time to test them all in this video.
So what I'll do is test the Category based methods.
First, the REST methods that use HTTP GET.
We've already seen the FindAll method through the
browser.
There are two additional methods:
* FindById * FindByName
I'll show you those now through the browser.
First, let's look at FindById.
To exercise this method, I'll change the URL to
http://localhost:8080/CategoryRestService/FindById/1
Where I'll pull the Category whose ID is 1.
And there it is.
Next, FindByName takes the name of the Category and returns it if it can be located.
http://localhost:8080/CategoryRestService/FindById/TEST_CATEGORY2
I'll use SoapUI to send JSON requests to the RESTful
methods of the application that take (HTTP) PUT, POST, and DELETE.
I've provided a SoapUI project as part of the
SpringBootDemo application.
Once Spring Boot comes up, I'll import the project that
lives in the SpringBootDemo folder.
CAPTION: Oops, Steve means to say "Once SoapUI comes up"
First, I'll exercise the CategoryRestService.Add method
which uses HTTP PUT by opening up the TEST_123 request.
Make sure the JSON tab is selected in the output pane.
Then I'll send the JSON request payload to the server:
{ "name":"TEST_123",
"description":"TEST_123_DESCRIPTION" }
If it works, I'll see this same object in the
JSON response.
And there it is:
{ "id": 5,
"whenCreated" : "2017-04-17", "whenLastUpdated" : "2017-04-17",
"name":"TEST_123", "description":"TEST_123_DESCRIPTION"
}
Just for fun, let's go to the browser and run the
FindAll method, and see if the Category we added shows
up.
And sure enough, there's the category object at the bottom.
{ "id": 5, "whenCreated" : "2017-04-17", "whenLastUpdated" : "2017-04-17", "name":"TEST_123", "description":"TEST_123_DESCRIPTION"}
Now for the Update method.
I'll exercise the CategoryRestService.Update method by
opening up the ID 5 request.
Make sure the Raw tab is selected in the output pane.
Then I'll send the JSON request payload to the server:
{ "id": 5,
"description": "TESTING_123_DESCRIPTION_UPDATED" }
If it works, I'll see "UPDATE SUCCESSFUL" in the
output pane.
And there it is:
UPDATE SUCCESSFUL
Just for fun, let's go to the browser and run the
FindById method for ID 5, and see if the update was reflected
in the DB.
And sure enough, there's the Category object with updated
description.
{ "id": 5, "whenCreated" : "2017-04-17", "whenLastUpdated" : "2017-04-17", "name":"TEST_123", "description":"TEST_123_DESCRIPTION_UPDATED"}
Finally, I'll exercise the Delete method by opening up the ID 5
request.
Make sure the Raw tab is selected in the output pane.
Then I'll send the JSON request payload to the server:
{ "id" : 5
}
If it works, I'll see the "DELETE SUCCESSFUL" in the output
pane.
And there it is:
DELETE SUCCESSFUL
Again, let's go to the browser and run the FindAll method,
and see that the Category was deleted.
Sure enough, the Category with ID 5 is no longer in the DB.
***********************************************************
That was a quick look at how to build, run, and test a
Spring Boot RESTful web application.
Spring Boot is a great way to build applications that
"just run."
If you found your way to this video through another path
than the IBM developerWorks Spring Boot tutorial, please
check out the video description, where you'll find a link
to the tutorial in case you want to check it out.
I hope you enjoyed this video, and thanks for watching.
I'm Steve Perry and I'll see you next time.
So long.
No comments:
Post a Comment