[JAVA] Spring with Kotorin --2 RestController and Data Class

Overview / Description

The code I saw in "Spring with Kotorin-- 1. SPRING INITIALIZR" was automatically generated by SPRING INITIALIZR and there is no particular processing. It was a thing.

So this time, I will add the following very simple process.

--Add controller for REST access: ** RestController ** --Adding entities for information to display on access: ** Data Class **

RestController Create a class with ** @ RestController ** annotations to define the RestController class.

RestController


@RestController
class SimpleController { 
  ...
}

By the way, this @RestController is a combination of ** @ Controller ** and ** @ ResponseBody **. Therefore, it behaves in the same way even if it is defined as follows.

Controller


@Controller
@ResponseBody
class SimpleController { 
  ...
}

RequestMapping ** @RequestMapping ** Annotate and map to the request from the client.

Specify the mapping condition using the following attributes.

GetMapping The ** @GetMapping ** annotation is an annotation that acts as a shortcut for @RequestMapping (method = RequestMethod.GET).

Data Class A data class is a class that is used to hold only data without processing. The data class is defined in the following format.

-** data ** Add keyword before class --Define the primary constructor

DataClass


data class Data(var id: String, var value: String)

The compiler automatically infers and generates the following processing from the contents defined as the data class.

Assumptions / Environment

Runtime version

Spring Dependencies

Development environment

Procedure / Explanation

Data Class

data class Message(var id: String,
                   var title: String,
                   var message: String)

You can see that the description is simple without getter, setter, toString, and equals that were described when defining the definition of JavaBeans in Java. A similar description was achieved in Java by using ** Lombok **, but Kotlin supports this format at the language level.

RestController

import io.pivotal.syanagihara.simple.data.Message
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.text.SimpleDateFormat
import java.util.*

@RestController
@RequestMapping("/simple")
class SimpleController {

    @GetMapping
    fun getMessages() : List<Message> {
        return listOf(
                Message(
                        UUID.randomUUID().toString(),
                        "First Message",
                        "This is a 1st message on ${getDate()}."
                )
        )
    }

    private fun getDate() : String {
        val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
        return simpleDateFormat.format(Date())
    }
}

Using Java API

I am using the ** java.util.Date ** class to print the current date and time. In this way, Java API calls from Kotlin can be made transparently.

Access modifier

I have declared getDate () private, but I will briefly summarize Kotlin's access modifiers.

Access modifier Contents
public Accessable from all classes
internal Accessable from classes in the same module
protected Only accessible from subclasses
private Only accessible from the declared class

Summary / Looking back

When accessing this application, the following result is returned.

$ curl http://localhost:8080/simple

[{"id":"2ac2bf0a-8276-43e2-89d2-c0eb70ad38d9","title":"First Message","message":"This is a 1st message on 2018/11/04 20:52:45."},{"id":"04e915e4-f4fb-4920-84c9-f411f7603044","title":"Second Message","message":"This is a 2nd message on 2018/11/04 20:52:45."}]

I find Lombok + Java useful, but I find Kotlin, which offers Lombok-equivalent functionality at the language level, even more useful. Also, this time I made a simple application using RestController. It's very simple and easy with Spring + Kotlin without any unnecessary description.

This source

Recommended Posts

Spring with Kotorin --2 RestController and Data Class
Spring with Kotorin --- 5. Actuator
Spring with Kotorin ―― 1. SPRING INITIALIZR
Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)
Spring with Kotorin --8 Repository layer
Spring with Kotorin --6 Asynchronous processing
Spring with Kotorin ―― 7. Service layer
Spring with Kotorin --4 REST API design
Spring with Kotorin --9 Database migration --Flyway
Data linkage with Spark and Cassandra
Creating REST APIs with Spring JPA Data with REST and Lombok incredibly easy.
OR search with Spring Data Jpa Specification
HTTPS with Spring Boot and Let's Encrypt
Form class validation test with Spring Boot
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
Just input and output images with Spring MVC
Class and model
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Write RestController tests quickly with Spring Boot + Spock
Periodically update DB with Spring Batch and MyBatis
Spring Data JPA Entity cross-reference and its notes
Spring with Kotorin --8 Repository layer --Supplement: H2 Database
[Beginner] Upload images and files with Spring [Self-satisfaction]
Sort by Spring Data JPA (with compound key sort)
Creating a common repository with Spring Data JPA
Form and process file and String data at the same time with Spring Boot + Java
Try using DI container with Laravel and Spring Boot
A memorandum when trying Spring Data JPA with STS
Switch environment with Spring Boot application.properties and @Profile annotation
Spring with Kotorin --3. Omitting curly braces from the function
Spring Security usage memo: Cooperation with Spring MVC and Boot
Spring Boot with Spring Security Filter settings and addictive points
I tried to get started with Spring Data JPA
Until the use of Spring Data and JPA Part 2
Difference between Spring AOP and library proxy target class
Until the use of Spring Data and JPA Part 1
Import OSM data into PostGIS and visualize with QGIS
Attempt to SSR Vue.js with Spring Boot and GraalJS
Connect Spring Boot and Angular type-safely with OpenAPI Generator
abstract (abstract class) and interface (interface)
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Spring Data JDBC Preview
Self-made Validation with Spring
Class and instant part2
With Kotorin ―― 7. Scoping Function
Download with Spring Boot
spring data dynamodb trap
Find the address class and address type from the IP address with Java
Window aggregation of sensor data with Apache Flink and Java 8
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Create API to send and receive Json data in Spring
Connect to database with spring boot + spring jpa and CRUD operation
Flow until output table data to view with Spring Boot
Implement REST API with Spring Boot and JPA (domain layer)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~