이번 포스팅에서는 간단하게 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방식을 브라우저 주소창에 입력하면
이렇게 에러가 뜬다.
다음 포스팅에서는 컨트롤러에서 쓰이는 어노테이션에 대해 작성해보겠다.
'Spring Boot' 카테고리의 다른 글
[Spring Boot] 개발환경 설정 -(4) 응답 객체 생성하기 (0) | 2022.06.02 |
---|---|
[Spring Boot] @Controller + @ResponseBody / @RestController (0) | 2022.05.30 |
[Spring Boot] 개발환경 설정 -(3) Swagger 적용하기 & Lombok 적용 (0) | 2022.05.28 |
[Spring Boot] 개발환경 설정 -(2) Git 연동하기 (0) | 2022.05.28 |
[Spring Boot] 개발환경 설정 -(1) 프로젝트 생성 (0) | 2022.05.28 |