My first steps in Java Spring. I spent my first NCR Edinburgh training days to have a jump start in Java Spring in order to learn more on this topic and be able to handle related tasks on my team.
As the blog post title suggests, this is my first steps in Java Spring. I spent my first NCR Edinburgh training days to have a jump start in Java Spring in order to learn more on this topic be able to handle related tasks on my team.
Apart from the old plain Java code, all other aspects were completely new to me, so I had to get familiar with them before I was able to produce something functional, however, I really enjoyed it and I am looking forward to learn more.
The output of this training was a simple Posts CRUD application in Java Spring.
I used IntelliJ and the first interesting and really useful thing I noticed was that when creating a new project through Spring Initializr, you get a nice interface where you can select the appropriate dependencies to populate the pom.xml
automatically.
For this example, I used Web
, H2
, JPA
and Thylemeaf
.
Having the project structure in place already first thing to do was to create my Post entity for which I used simple Java code and a few Spring annotations.
For this example, I used the @Entity
, @Id
and @GeneratedValue
annotations to declare a Post entity with a message and an auto generated incremented ID and their getters and setters.
In order connect my post entity to the actual H2 database entries and support CRUD functionality, I used repository interface, another useful Spring component that brings a lot of methods like list, delete etc.
Next, moved to the controller where I added basic functions to handle the requests.
Again used a few annotations that brought the required libraries to my file and set the routing configuration.
For instance, in order to support the deletion of a post all I had to do was to set up the /{id}/delete
route with @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
annotation just above my function and inside it perform a delete on the post repository and finally redirect to my /posts route where all posts are listed.
Finally, for my views I used basic HTML and a simple CSS bootstrap template. In more detail, I had populated a layout with the bootstrap template containing a navigation bar at the top, a container and a layout fragment inside it where the views are injected from Thymeleaf in the middle of the page and a footer at the bottom.
In order to run my application locally all I had to do was to run mvnw spring-boot:run
which spinned up a tomcat instance on port 8080.
This was a really interesting training for me, learned a couple of useful things admired the power of the tools I used and I am looking forward to learn more and apply it on daily activities within the team.
Andrew Basekis
Tags: java spring
March 3rd 2017