Story of Jr. Backend/DevOps Developer

Story of Jr. Backend/DevOps Developer.
ko en

3. Get start with Spring: Create simple GET API with SpringBoot.

2023-01-13 spring-study

Intro

This blog article aims to summarize or organize information that I’ve learned from Inflearn Lecture

Lecture does explain about SpringBoot using JAVA, but I am trying to organize information with my knowledge, especially comparing with ASP.NET Core(since ASP.NET Core is framework that I’m really familiar of.) and using Kotlin.

Key points of aritcle.

  • Create Simple API Controller.
  • Comparing with ASP.NET Core(The one I’m familiar of.)

Let’s create simple API Controller.

Brief Intro.

I think this example/code will be very easy if the one is familiar of other backend frameworks/languages.

Code

package com.kangdroid.initspringboot.controllers

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody

@Controller // 1. Controller Class
class HelloController {

    @GetMapping("api/hello") // 2. api/hello Route Match
    @ResponseBody // 3. Response Type is application/json (object)
    fun helloApi(): UserProjection {
        return UserProjection(
                nickName = "KangDroid",
                email = "kangdroid@test.com",
                role = "Role.Developer"
        )
    }
}

data class UserProjection(
        val nickName: String,
        val email: String,
        val role: String
)

Run Result

Comparing with ASP.NET Core

First, Code(C#)

public class UserProjection 
{
    public string NickName { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
}

[ApiController]
public class HelloController: ControllerBase 
{
    [HttpGet("/api/hello")]
    public ActionResult HelloApi()
    {
        return Ok(new UserProjection 
        {
            NickName = "KangDroid",
            Email = "kangdroid@test.com",
            Role = "Role.Developer"
        });
    }
}

Thoughts.

Actually example code I wrote above is some kind of getting static object - not really complex, no middleware or filters. Therefore I cannot really say what is more comfortable to me, but I can say There are something in common!

Both languages/frameworks have some commonalities,

  • Attribute/Annotation that the class acts as a Controller ([ApiController] vs @Controller)
  • Attribute/Annotation defining HTTP method and routing ([HttpGet("route-path")] vs @GetMapping("route-path"))
  • Whether the response value includes the object we want (UserProjection)

Now, these parts are the comparison of the front-end part of the API logic, excluding the middleware/filter. I look forward to analyzing the middleware/filter concept or the actual DB logic part as I learn a little more in the future.