@RestController//restapi처리하는 컨트롤러 지정
@RequestMapping("/api")//api로 시작하는 주소의 요청은 여기서 처리
public class RestAPIController {
//클라이언트가 특정 요청을 보내주기 위해 서버의 진입점이 어떠한 주소를 가지는 지를 작성
//어떠한 주소를 받겠다를 설정
//응답을 내려줬고, 문자열로 전달을 했다. 0 또는 1이라는 비트 단위의 데이터로 전달한다.
@GetMapping(path = "/hello")//hello라는 주소는 이 메소드가 처리하겠다.
public String hello(){
var html = "<html> <body> <h1> Hello Spring-Boot </h1> </body> </html>";
return html;
}
@GetMapping(path = "/echo/{message}/age/{age}/is-man/{isMan}") //pathvariable에서 메세지와 값이 다르면 안됨->(name = "message")를 통해 해결
public String echo(
@PathVariable(name = "message") String msg,
@PathVariable int age,
@PathVariable boolean isMan
){ //메세지에 해당하는 값들을 문자로 파싱
System.out.println("echo message: "+msg);
System.out.println("echo message: "+age);
System.out.println("echo message: "+isMan);//서버에서 어떻게 전송되는지 보여줌
@GetMapping(path = "/book")
public void queryParam(
//request 파라미터로 들어오는 것을 매칭시키겠다.
@RequestParam String category,
@RequestParam String issuedYear,
@RequestParam (name="issued-month") String issuedMonth,
@RequestParam String issued_day//snake케이스는 추천하지 않는다.
){
System.out.println(category);
System.out.println(issuedYear);
System.out.println(issuedMonth);
System.out.println(issued_day);
}
@GetMapping(path = "/book2") //주소와 변수의 값을 일치 시켜주어야 한다.
public void queryParamDto(
//request 파라미터로 들어오는 것을 매칭시키겠다.
BookQueryParam bookQueryParam //아무런 어노테이션을 붙이지 않는다.
){
System.out.println(bookQueryParam);
}
@RequestParam 어노테이션을 사용해서 파라미터로 들어오는 것을 선언한 변수와 매칭시키겠다.
객체를 받을 때는 어노테이션을 명시하지 않는다.
파라미터에 들어갈 변수명과 동일한 매칭이 되지 않을 경우 자바에서는 '-'으로 변수를 선언할 수 없기 때문에 (name="issued-month")으로 지정해서 사용해줘야함.
어노테이션으로, 롬복을 사용하면 Getter, Setter, 생성자, toString() 등을 선언하지 않아도 자동으로 생성되어 사용할 수 있음.
코드가 직접 눈에 보이는게 아니므로 직관성이 떨어질수 있습니다.