[Springboot] RESTful GET 해보기!

@욕심쟁이

·

2022. 1. 18. 23:38

반응형
package com.example.springboot202111.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.springboot202111.dto.UserRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/get")
public class GetController {

    //@RequestMapping("/hi") // get/ post / put / delete 모든 RESTful 동작
    //@RequestMapping(path = "/hi", method = RequestMethod.GET) // 메서드 설정시  get http//localhost:8000/api/get/hi
    //@GetMapping(path = "getHello") : 명시적
    @GetMapping("/getHello")    // http://localhost:8000/api/get/getHello
    public String getHello(){
        return "get Hello";
    };

    //변화하는 부분은 @PathVariable을 사용
    //public String pathVariale(@PathVariable String name){
    // @PathVariable(name = "name") name 변수명을 다르게 설정
    @GetMapping("/pathVariale/{name}") // ://localhost:8080/api/get/pathVariale/내가 입력한 path
    public String pathVariale(@PathVariable(name = "name") String pathName){
        System.out.println("PathVariable : " + pathName );
        return pathName;
    };

    //http://localhost:8000/api/get/queryParam?user=jiyeon&email=jiyoen@naver.com&age=20
    @GetMapping(path = "/queryParam")
    public String queryParam(@RequestParam Map<String, String > queryParams){
        StringBuilder sb = new StringBuilder();
        queryParams.entrySet().forEach(val -> {
            System.out.println(val.getKey());
            System.out.println(val.getValue());
            System.out.println("\n");

            sb.append(val.getKey() +" = "+ val.getValue());

        });
        return sb.toString();
    };

    //http://localhost:8000/api/get/queryParam2?user=jiyeon&email=jiyoen@naver.com&age=20
    @GetMapping(path = "/queryParam2")
    public String queryParam2(
            @RequestParam String user,
            @RequestParam String email,
            @RequestParam int age
    ){
        System.out.println(user);
        System.out.println(email);
        System.out.println(age);
        return user +" "+ email +" "+ age;
    };
    //가장 많이 사용
    //http://localhost:8000/api/get/queryParam3?user=jiyeon&email=jiyoen@naver.com&age=20
    @GetMapping(path = "/queryParam3")
    public String queryParam3(UserRequest userRequest){
        System.out.println(userRequest.getUser());
        System.out.println(userRequest.getEmail());
        System.out.println(userRequest.getAge());
        return userRequest.toString();
    };
}
package com.example.springboot202111;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot202111Application {

    public static void main(String[] args) {

        SpringApplication.run(Springboot202111Application.class, args);
    }

}

반응형

'IT > Spring' 카테고리의 다른 글

[Spring] AOP란?  (0) 2022.01.22
[Spring] 주요 Annotation  (0) 2022.01.19
[Springboot] RESTful POST 해보기!  (0) 2021.11.06
[Springboot] 프로젝트 생성하기  (0) 2021.11.05