Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package likelion.spring_prac;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

// 정적
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}

// MVC (파라미터로 값 받기)
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(value = "name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}

// API 단순 예
@GetMapping("hello-string")
@ResponseBody // HTTP의 바디 부분에 data를 직접 넣겠다
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}

// API 사용 예
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello(); // 객체 생성
hello.setName(name);
return hello;
}

static class Hello {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Loading