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).


@GetMapping("/")
fun getAll(): MutableMap{
	...
}

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


@PostMapping("/post")
fun postCustomer(@RequestBody customer: Customer): String{
	...
}

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


@PutMapping("/put/{id}")
fun putCustomer(@PathVariable id: Long, @RequestBody customer: Customer): String{
	...
}

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

 
@DeleteMapping("/delete/{id}")
fun deleteMethod(@PathVariable id: Long): String {
	...
}

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:

<properties>
	<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
	<kotlin.version>1.1.4</kotlin.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.jetbrains.kotlin</groupId>
		<artifactId>kotlin-stdlib-jre8</artifactId>
		<version>${kotlin.version}</version>
	</dependency>
	<dependency>
		<groupId>org.jetbrains.kotlin</groupId>
		<artifactId>kotlin-reflect</artifactId>
		<version>${kotlin.version}</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

2. Create Kotlin data model

– Create a Kotlin Customer.kt data class:


package com.javasampleapproach.requestmapping.model

data class Customer(
		var id: Long = -1,
		var firstName: String = "",
		var lastName: String = "",
		var age: Int = -1) {
}

3. Implement RestController

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


package com.javasampleapproach.requestmapping.controller

import com.javasampleapproach.requestmapping.model.Customer
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import javax.annotation.PostConstruct

@RestController
@RequestMapping(value="/api/customer")
class WebController {
	// Define a customer storage
	val custStores = mutableMapOf()
	
	@PostConstruct
    fun initial(){
        custStores.put(1, Customer(1, "Jack", "Smith", 20))
        custStores.put(2, Customer(2, "Peter", "Johnson", 25))
    }
	
	@GetMapping("/")
    fun getAll(): MutableMap{
		println("########### GET All Customers: ${custStores}")
		return custStores
    }
	
	@GetMapping("/get")
    fun getCustomer(@RequestParam("id") id: Long): Customer{
		val cust = custStores.getValue(id);
		println("########### GET a Customers with ${cust}")
		return cust
    }
 
    @PostMapping("/post")
    fun postCustomer(@RequestBody customer: Customer): String{
		// do post
		custStores.put(customer.id, customer)
		
		// log on console
		println("########### POST:" + customer)
		
        return "Post Sucessfully!"
    }
 
    @PutMapping("/put/{id}")
    fun putCustomer(@PathVariable id: Long, @RequestBody customer: Customer): String{
		// reset customer.Id
		customer.id = id;
		
		if(custStores.get(id) != null){
			custStores.replace(id, customer)
		}else{
			customer.id = id
			custStores.put(id, customer)
		}
		
		println("########### PUT:" + customer)
		return "Put Sucessfully!"
    }
 
    @DeleteMapping("/delete/{id}")
    fun deleteMethod(@PathVariable id: Long): String {
		val cust = custStores.remove(id)
		if(cust != null){
			println("########### DELETE:" + cust)
		}else{
			println("########### Don't exist any customer with id = ${id}")
		}
		
		return "Done!"
    }
}

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

Leave a Reply

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