
API는 "Application Programming Interface"의 약자로, 소프트웨어 응용 프로그램이 서로 통신하는 데 사용되는 규칙과 도구 모음이다.
API는 프로그램이나 애플리케이션이 다른 프로그램이나 애플리케이션의 기능을 사용할 수 있게 할 수 있어 확장성이 좋다.
기존에 있던 HelloController에 컨트롤러를 추가했다.
// contoller/HelloController.java
...
import org.springframework.web.bind.annotation.ResponseBody;
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
@ResponseBody가 있다면 Http 통신의 Body에 return값을 포함해 반환하게 된다.
따라서 다음과 같이 파라미터로 넘겨준 값이 출력된다.

위 사진만 본다면 다른 방식과 똑같아 보인다.
하지만 출력된 웹 페이지에서 페이지 소스를 보면 Html이 아닌 반환값만 출력이 되는 것을 볼 수 있다.

하지만 Api의 진가는 위와 같은 단순한 문자를 반환하는 것이 아닌 Json 데이터를 반환하는 것이라고 한다.
그래서 다시 컨트롤러를 추가했다.
// contoller/HelloController.java
...
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello;
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Hello라는 객체를 생성하고 객체에 name값을 담아 객체 그대로 반환한다.
Spring Boot에서는 @ResponseBody를 사용하여 객체를 반환하면 Spring의 메시지 컨버터가 자동으로 객체를 JSON 형식으로 변환해준다고 한다.

name 파라미터에 "자바스프링입니다" 를 전달했고 위와 같이 Json형태로 출력된 것을 확인 할 수 있었다.