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:


	true
	UTF-8
	UTF-8
	1.8
	1.1.4



	
		org.springframework.boot
		spring-boot-starter
	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.jetbrains.kotlin
		kotlin-stdlib-jre8
		${kotlin.version}
	
	
		org.jetbrains.kotlin
		kotlin-reflect
		${kotlin.version}
	

	
		org.springframework.boot
		spring-boot-starter-test
		test
	

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



By grokonez | September 1, 2017.


Related Posts


Got Something To Say:

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

*