Kotlin Spring MVC RequestMapping RESTful APIs with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping | SpringBoot Example

In the tutorial, JavaSampleApproach shows you how to create Kotlin Spring MVC RequestMapping RESTful APIs with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping using SpringBoot.

Related posts:
Kotlin SpringJPA Many-To-Many relationship
Kotlin Spring JPA + Postgresql | Spring Boot Example
Kotlin SpringJPA Hibernate One-To-Many relationship
Spring Framework 4.3 New Feature RequestMapping: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6.RELEASE
– Kotlin 1.1.4
– PostMan – Rest Client

II. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

@ org.springframework.web.bind.annotation.GetMapping
@GetMapping acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

@ org.springframework.web.bind.annotation.PostMapping
@PostMapping acts as a shortcut for @RequestMapping(method = RequestMethod.POST).

@ org.springframework.web.bind.annotation.PutMapping
@PutMapping acts as a shortcut for @RequestMapping(method = RequestMethod.PUT).

@ org.springframework.web.bind.annotation.DeleteMapping
@DeleteMapping acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE).

III. Practice

In the tutorial, We use SpringToolSuite to create a Koltin SpringBoot project:

kotlin springmvc requestmapping - project structure

Step to do:
– Create Kotlin SpringBoot project
– Create Kotlin data model
– Implement RestController
– Run and check results

1. Create Kotlin SpringBoot project

We use SpringToolSuite to create a SpringBoot with Kotlin language:

kotlin springmvc requestmapping - create kotlin project

Then add web dependency:

2. Create Kotlin data model

– Create a Kotlin Customer.kt data class:

3. Implement RestController

We implement a Kotlin RestController WebController.kt with 5 RESTful APIs:

4. Run and check results

Build and Run the Kotlin SpringBoot project with commandlines {mvn clean install, mvn spring-boot:run}.

– Make a Get RequestMapping to get all customers http://localhost:8080/api/customer/ -> Result:

– Make a Get RequestMapping to get a customer with id = 1 http://localhost:8080/api/customer/get?id=1 -> Result:

kotlin springmvc requestmapping - get a customer by id

– Make a Post RequestMapping to add a new customer http://localhost:8080/api/customer/post -> Result:

kotlin springmvc requestmapping - post a customer

– Make a Put RequestMapping to modify a customer http://localhost:8080/api/customer/put/3 -> Result:

kotlin springmvc requestmapping - put a customer

– Make a Delete RequestMapping to remove a customer http://localhost:8080/api/customer/delete/1 -> Result:

kotlin springmvc requestmapping - delete a customer by id

– For final check, again make a Get RequestMapping to get all customers http://localhost:8080/api/customer/ -> Result:

kotlin springmvc requestmapping - final get all

-> Server’s Logs:

kotlin springmvc requestmapping - backend logs

IV. Sourcecode

KotlinSpringMVCRequestMapping


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*