On Property and Liberty

If one is to have liberty, should it include the liberty to do as he wishes with his own property? Can restrictions be justifiably placed on one’s use of his own property for the purpose of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A Very Beginner journey with Kotlin and Spring Boot

I recently started learning Kotlin and Spring. I will, through a series of articles, try to track my progress.

This part is an intoduction for the very basic steps to create a Spring Boot project with kotlin.

Kotlin

Kotlin is a programming language created by JetBrains in 2010 and became an official language on Android.

”It’s concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today: for server-side development, Android apps, and much more. Kotlin works great with all existing Java libraries and frameworks and runs with the same level of performance as Java”, Kotlin in Action, by Dmitry Jemerov and Svetlana Isakova

Spring

“ Spring is a complete and modular framework used for the development of any java based applications, it focuses on the “plumbing” of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments ”. More details in

Spring Boot

“ Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring”

Create a Spring Boot project

· The type of the project(Maven or Gradle), this article explains the difference between Gradle and Maven .

· Programming language : kotlin

· Spring Boot version, until wrting this tutorial the latest stable version is : 1.5.10

· Dependencies : for now we need the web dependency

Application class

“This class is the main class, This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication bootstraps our application, starting Spring”

application.properties

By default Spring boot applicatin runs on 8080, to change this port the following property should be added to application.properties file

server.port=new port

In my case i changed it to 9095

HelloController

“In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the@RestController annotation ”

In HelloController the annotation @GetMapping() ensures that HTTP request to /hello is mapped to the greeting() method and @RequestParam binds the value of the query string parameter name into the name parameter of the greeting method

The application can now be tested in the browser via the URL :

The full project can be found in hello-kotlin-spring

Adding a service

In this part a new project spring-kotlin-with-service was created following the same steps as the first project, In this project a UserService interface and a UserServiceImp were added.

The service implementation should be a Spring bean, it has to have a @component or @service annotation so that Spring will find it and register it in the Spring application context.

Then you use dependency injection, through the @Autowired annotation, to inject the implementation of the service into the controller. This means that Spring will look at your controller, it will find the @Autowired annotation on the service member variable and initialize it with a bean that it finds in the application context, which will be the instance of the service implementation class that it has registered earlier. So, after Spring is done, service will refer to the instance of serviceImpl

Data Class

UserController

The UserController serves requests of type Get, Post, Put and Delete and returns a JSON object representing an instance of User, for more information about JSON format:

@GetMapping : used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

@PostMapping : used to send data to the server

@PutMapping : used to replace the current data

@DeleteMapping : used to delete data

The LOGGER allows messages to be written to the log using the methods which represent each logging level,trace, debug, info, warn, error followed be the message. The braces / curly brackets will be replaced by the value passed in as a method parameter

To configure the logging an XML file (logback-default.xml) was added to resources :

This sets the global level to DEBUG and everything from the code to INFO. This logger will be injected in the UserServiceImpl.

UserService

UserServiceImpl

This class contains the implementation of the services declared in the UserService interface

The full implementation can be found in spring-kotlin-with-services

The application can now be tested in Postman

Get request

Post request

Put request

Delete request

Add a comment

Related posts:

Giving Birth to a New Idea

Fighting against stagnant ideas that have locked up humanity’s potential. On a journey of individual and collective realisation. Fostering the Feline spirit that possesses curious, mysterious, and…

American Gun Violence in the Eyes of a Spaniard

A week after the devastating events at Marjory Stoneman Douglas High School, where 17 people were killed by an unstable lunatic who was able to legally buy war weapons, lawmakers are busy trying to…

The Conscious Night

Tell me something. Something dark.. “The Conscious Night” is published by Stephen Schatzl.