RESTful 할려면 바로 브라우저에 찍히게 해야함 responsebody 이용! (RESTful은 ajax에서 사용해야함)
package com.jeungsu.test.contoller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class JeongsuController {
@GetMapping("/") // 여기에 @RequestMapping 쓰지 않아요!
public String home() {
return "home";
}
@GetMapping("/jeongsu")
@ResponseBody // 요게 붙으면 바로 직접 답변!
public String restGet() {
return "박정수";
}
}

package com.jeungsu.test.contoller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class JeongsuController {
@GetMapping("/") // 여기에 @RequestMapping 쓰지 않아요!
public String home() {
return "home";
}
@GetMapping("/jeongsu")
// @ResponseBody // 요게 붙으면 바로 직접 답변!
public void restGet(HttpServletResponse res) throws IOException {
res.getWriter().write("jeongsu JJANG");
// return "박정수";
}
}



실제 요청 URL을 해보면 값이 잘 뜨는 것을 확인할 수 있다.

package com.jeungsu.test.contoller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
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.ResponseBody;
@Controller
@RequestMapping("/")
public class JeongsuController {
@GetMapping("/") // 여기에 @RequestMapping 쓰지 않아요!
public String home() {
return "home";
}
@GetMapping("/jeongsu/{num}")
@ResponseBody // 요게 붙으면 바로 직접 답변!
public String restGet(@PathVariable int num){
if(num == 1) {
return "박정수";
}
return "고영우";
}
}
