Spring Boot

[Spring Boot] Postman 으로 api 테스트해보기

DEV_HEO 2022. 5. 30. 23:03
320x100

 

이번 포스팅에서는 간단하게 postman으로 "Hello World!"를 출력하는 api를 작성해보겠다.

 


1. Download Postman

 

https://www.postman.com/downloads/

 

Download Postman | Get Started for Free

Try Postman for free! Join 20 million developers who rely on Postman, the collaboration platform for API development. Create better APIs—faster.

www.postman.com

 

 

 

위 사이트 들어가서 postman을 다운받는다.

 

 


2. TestController 작성

@RestController
@RequestMapping("/api")
public class RestTestController {
	
	@GetMapping("/getTest")
	public String getTest(){
		return "Hello World!";
	}
	
	@PostMapping("/postTest")
	public String postTest() {
		return "Hello World!";
	}
}

 

 

@RequestMapping("/api")를 지우고

 

@GetMapping("/api/getTest")

@PostMapping("/api/postTest")

로 입력해도 같은 결과가 나타난다.

실무에서는 보통 공통되는 url를 /api/sample 처럼 적어준다.

 


3. Postman 테스트

 

순서대로

http://localhost:8080/api/getTest

http://localhost:8080/api/postTest

를 Postman url에 입력해준다.

 

 

 

- Get 

http://localhost:8080/api/getTest => Send클릭

 

 

- Post

http://localhost:8080/api/postTest => Send클릭

 

 

 

 

Get은 브라우저에 직접 url을 입력하는 것과 똑같다.

 

하지만 Post방식을 브라우저 주소창에 입력하면

이렇게 에러가 뜬다.

 


다음 포스팅에서는 컨트롤러에서 쓰이는 어노테이션에 대해 작성해보겠다.

320x100