Spring/이론

get방식--요청 시 사용 // talend Api 구글확장프로그램//@RestController//@GetMapping()//@RequestMapping("/api/v1/get-api")//@PathVariable//

SEOKIHOUSE 2023. 7. 31. 15:38

구글확장프로그램 추가

복사하고


  • GET API  웹 애플리케이션 서버에서 값을 가져올 때 사용하는 API(파라미터 용도: 자료를 지정)

@RestController: ResponseBody생략 가능 - > view를 제공x -> 데이터만 내보낼 경우 (ex)- 공공데이터)


  • @RequestMapping("/api/v1/get-api"): 해당 컨트롤러의 모든 핸들러 메서드가 "/api/v1/get-api" 경로를 처리하도록 지정
  • value (또는 path): 요청을 처리할 URL 경로를 지정
  • method=RequestMethod.GET으로 설정되어 있으므로, getHello() 메서드는 GET 요청에 응답하도록 지정


@PathVariable

아래처럼해도된다




practice0731_ApiTest.zip
0.09MB

package com.study.springboot;

import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.study.springboot.dto.MemberDto;

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

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String getHello() {
		return "Hello world";
	}

	@GetMapping(value = "/name")
	public String getName() {
		return "짱구";
	}

	@GetMapping(value = "/variable1/{variable}")
	public String getVariable(@PathVariable String variable) {
		return variable;
	}

	@GetMapping(value = "/variable2/{variable}")
	public String getVariable2(@PathVariable("variable") String var) {
		return var;
	}

	@GetMapping(value = "/request1")
	public String getRequestParam1(
			@RequestParam String name,
			@RequestParam String email,
			@RequestParam  String orgainzation) {
		return name + "|" + email + "|" + orgainzation;
	}
	
	@GetMapping(value="/request2")
	public String getRequestParam2(@RequestParam Map<String, String> param) {
		StringBuilder sb = new StringBuilder();
		param.entrySet().forEach(map -> {
			sb.append(map.getKey() + " : " + map.getValue() + "\n");
		});
		
		return sb.toString();
	}
	
	@GetMapping(value="/request3")
	public String getRequestParam3(MemberDto member) {
		return member.toString();
	}
}