@RestController
@RequestMapping("/api/v1/get-api")
{
public class GetController{
@RequestMapping(value="/hello" ,method= RequestMethod.Get)
public String getHello(){
return "Hello World";
}
}
}
@RequestMapping 어노테이션을 사용하는 방법이다. 단지 RequestMapping 어노테이션을 사용할경우 모든 method에 대해 응답을한다. method에 대한 응답을주려면 method 값으로 지정해주어야한다. 위의 예제는 GET 메소드를 이용하는 방법의 예시 이다.
하지만 스프링 4.3 버전이후로 새로나온 아래의 어ㅏ노테이션을 사용하기에 @RequestMapping 어노테이션은 더 이상 사용되지 않는다.
- @GetMapping :get메소드에 대한 응답
- @PostMapping : post메소드에 대한 응답
- @PutMapping : put메소드에 대한 응답
- @DeleteMapping : delete메소드에 대한 응답
@GetMapping("/variable1/{variable}")
public String getVariable1(@PathVariable String variable){
return variable;
}
/* localhost:8080/variable1/12345
=> variable =12345
*/
@GetMapping("/request1")
public String getRequestParam1(
@RequestParam String name,
@RequestParam String email,
@RequestParam String organization)
{
return name+email+organization;
}
)
/*
localhost:8080/request1?name=jimin&email=test@gmail.com&organization=company
name,email, organization 을 key value 형태로 매핑
*/