π[Spring] API.
'@ResponseBody λ¬Έμ λ°ν'
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
}
-
'@ResponseBody'
λ₯Ό μ¬μ©νλ©΄ λ·° 리쑸λ²('viewResolver'
)λ₯Ό μ¬μ©νμ§ μμ΅λλ€.- λμ μ HTTPμ BODYμ λ¬Έμ λ΄μ©μ μ§μ λ°νν©λλ€.(HTML BODY TAGλ₯Ό λ§νλ κ²μ΄ μλλλ€.)
-
μ€ν
- http://localhost:8080/hello-string?name=spring
'@ResponseBody κ°μ²΄ λ°ν'
@Controller
public class HelloController {
@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;
}
}
}
-
μ€ν
- http://localhost:8080/hello-api?name=spring
'@ResponseBody μ¬μ© μ리'
-
'@ResponseBody'
λ₯Ό μ¬μ©- HTTPμ BODYμ λ¬Έμ λ΄μ©μ μ§μ λ°ν
-
'viewResolver'
λμ μ'HttpMessageConverter'
κ° λμ - κΈ°λ³Έ λ¬Έμμ²λ¦¬:
'StringHttpMessageConverter'
- κΈ°λ³Έ κ°μ²΄μ²λ¦¬:
'MappingJackson2HttpMessageConverter'
- byte μ²λ¦¬ λ±λ± κΈ°ν μ¬λ¬
'HttpMessageConverter'
κ° κΈ°λ³ΈμΌλ‘ λ±λ‘λμ΄ μμ.
πββοΈ μ°Έκ³ : ** ν΄λΌμ΄μΈνΈμ HTTP Accept ν€λμ μλ²μ 컨νΈλ‘€λ¬ λ°ν νμ μ 보 λμ μ‘°ν©ν΄μ
**'HttpMessageConverter'
κ° μ νλλ€.