본문 바로가기

Spring

Spring REST API 기본호출 구현

  • HelloWorldController 를 만들어 기본적인 REST API 연결 확인
    • 함수명 : helloWorld
    • 리턴값 : String
    • Parameter 값 : 없음
    • Method 상단에 @GetMapping(path ="/hello-world") 추가
    • 호출 URL : http://localhost:8088/hello-world
    • Reponse : HttpStatus = 200 / Value = Hello World
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.GetMapping;

    @RestController
    public class HelloWorldController {
            //GET
        // /hello-world (endpoint)
        //@RequestMapping(mehtod=RequestMethod.Get, path="/hello-world")
        @GetMapping(path ="/hello-world")
        public String helloWorld(){
            return "Hello World";
        }
    }

  • HelloWorldController 에 Bean을 추가하여 REST API 호출
    • 함수명 : helloWorldBean
    • 리턴값 : HelloWorldBean
    • Parameter 값 : 없음
    • Method 상단에 @GetMapping(path ="/hello-world-bean") 추가
    • 호출 URL : http://localhost:8088/hello-world-bean
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.GetMapping;

    @RestController
    public class HelloWorldController {
            //GET
        // /hello-world (endpoint)
        //@RequestMapping(mehtod=RequestMethod.Get, path="/hello-world")
        @GetMapping(path ="/hello-world")
        public String helloWorld(){
            return "Hello World";
        }

            @GetMapping(path ="/hello-world-bean")
        public HelloWorldBean helloWorldBean(){
            return new HelloWorldBean("Hello World");
        }
    }

  • HelloWorldBean 추가
    • 변수타입 / 변수명 : String / message
    • Lombok Annotation으로 setter / getter / toString 생성 → @Data
    • Lombok Annotation으로 Contructor생성 → @AllArgsContructor
    package com.example.restfulwebservice.helloworld;

    import lombok.AllArgsConstructor;
    import lombok.Data;

    @Data 
    @AllArgsConstructor
    public class HelloWorldBean {
        private String message;
    }
    {
        "message": "Hello World"
    }

  • PathVariable 사용
    • HelloWorldControllerhelloWorldBean 메서드 추가
    • 함수명 : helloWorldBean
    • 리턴값 : HelloWorldBean
    • Parameter 값 : String name
    • Method 상단에 @GetMapping(path ="/hello-world-bean/path-variable/{name}") 추가
    • Parameter에 @PathVariable(value="name") 추가
    • 호출 URL : http://localhost:8088/hello-world-bean/path-variable/abcdef
    import org.springframework.web.bind.annotation.PathVariable;

    @GetMapping(path ="/hello-world-bean/path-variable/{name}")
    public HelloWorldBean helloWorldBean(@PathVariable(value="name") String name){
        return new HelloWorldBean(String.format("Hello World, %s",name));
    }
    {
        "message": "Hello World, a"
    }{
        "message": "Hello World, abcdef"
    }

'Spring' 카테고리의 다른 글

Spring Boot Start  (0) 2021.06.10
Lombok  (0) 2021.06.09
Cross-Origin-Resource-Sharing(CORS) 처리  (0) 2020.07.26
JUnit All Test 생성  (0) 2020.07.26
3-tier Architecture  (0) 2020.07.21