Spring Boot - API

package hello.hellospring.controller;

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

@Controller
public class HelloController {

    @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;
        }
    }
}

 

@ResponseBody를 사용하면 View Resolver를 사용하지 않습니다.

대신에 HTTP에 문자 내용을 직접 반환합니다.

먼저 hello-api URL을 전달받고

Hello 클래스에서 전달받은 파라미터로 name을 설정해 줍니다.

 

그리고 json 형식으로 hello를 반환해 줍니다.

'Java > Springboot' 카테고리의 다른 글

Spring Boot - jsp 사용하기  (0) 2025.02.25
Spring Boot - 빌드하고 실행하기  (1) 2025.02.21
Spring Boot - MVC 2  (0) 2025.02.21
Spring Boot - MVC 1  (0) 2025.02.21
Spring Boot - 프로젝트 생성  (1) 2025.02.20