스프링 입문 - 웹 개발 기초

이연희·2022년 6월 29일
0

Inflearn_Spring_Study

목록 보기
2/3

Inflearn Spring Study

웹 개발 기초

정적 컨텐츠

resource/static/hello-static.html을 추가한 결과

스프링 컨트롤러가 파일을 찾아본 뒤 매핑이 된 컨텐츠가 없기 때문에 내부 resources안에 있는 html 파일을 찾는다.

MVC와 템플릿 엔진

MVC: Model, View, Controller

모델: 내부적인 로직 처리
컨트롤러: 비지니스 로직 처리
뷰: 화면을 그리는 데에 집중

API

@ResponseBody: 뷰 리졸버를 사용하지 않고 http 바디에 문자를 그대로 전달

@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
    return "hello "+name;
}

객체를 전달하는 경우: HttpMessageConverter가 동작, JsonConverter를 통해 JSON 형태로 변경

@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;
    }
}
profile
공부기록

0개의 댓글