Kotlin

Kotlin Springboot Rest API 만들기 2 - HelloWorld, REST API TEST CLIENT 사용하기

pepega 2023. 1. 3. 20:08

전체 코드

https://github.com/GHGHGHKO/pepega-blog-kotlin

 

이전 포스팅에서는

localhost:8080에

Username, Password를 입력하여

 

Whitelabel Error page를 확인했습니다.

 

이번 포스팅에서는

HelloWorld API를 개발 후

Postman을 활용하여 API를 호출합니다.

 

 

1. HelloWorldController 생성

package 이름 밑에 (본 글에서는 com.example.pepega)

helloworld package를 생성하고

그 안에 HelloWorldController.kt를 생성합니다.

 

@Controller, @RestController의 차이

https://dncjf64.tistory.com/288

 

@Controller와 @RestController의 차이점

1.개요 Spring MVC의 @RestController은 @Controller와 @ResponseBody의 조합입니다. Spring 프레임 워크에서 RESTful 웹 서비스를 보다 쉽게 개발할 수 있도록 Spring 4.0에서 추가되었습니다. 근본적인 차이점은 @Contr

dncjf64.tistory.com

 

 

2. 프로젝트 실행

 

이전 포스팅에서 진행했던 대로

Username, Password를 입력합니다.

Username : user, Password: Using generated security password

 

 

이후

http://localhost:8080/helloWorld/v1/string

 

주소에 들어가면

https://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world

Hello World가 확인됩니다.

 

 

3. REST API TEST CLIENT 로 실행하기 (Postman)

본 글에서는 Postman으로 API를 호출하려고 합니다.

다른 Open source rest api test client도 많습니다

https://techbeacon.com/app-dev-testing/11-top-open-source-api-testing-tools-what-your-team-needs-know

 

11 top open-source API testing tools: What your team needs to know | TechBeacon

With microservices behind most newer development, API testing is becoming critical. Good thing AI is here to round out your testing strategy.

techbeacon.com

 

 

Postman에서 아래와 같이 설정합니다.

 

API를 호출하였으나

401 Unauthorized 오류가 확인되고 Hello World가 나오지 않습니다.

 

Spring Security 때문에 API가 호출되지 않습니다.

Using generated security password 를 입력해야 합니다.

 

Authorization -> Type -> Basic Auth -> Username, Password 입력

설정 후

Send하니 Response body가 확인됩니다.

 

 

다음 포스팅에서는

JPA에서 제일 중요한  Entity를 생성해보겠습니다.

https://pepega.tistory.com/59

 

추후 해당 계정으로

토큰을 발급하여

토큰으로만 다른 API를 호출할 수 있도록 연계 할 예정입니다.