[스프링] 웹 개발 기초 3

기록지·2022년 12월 19일
0

스프링

목록 보기
3/4
post-thumbnail
package hello.hellospring.controller;

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";
    }

    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }

    @GetMapping("hello-api")
    @ResponseBody //객체는 json 으로 기본 반환한다.
    //스프링에서 api 방식은 객체를 json 스타일로 반환해주는 방식을 의미한다.
    //api를 보통 이런식으로 활용함.
    public Hello helloApi(@RequestParam("name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }

    static class Hello{ //alt + insert 자동 getter/setter
        // property 접근방식/ 자바빈 규약(표준방식)이라고 함.
        private String name;

        public String getName() {
            return name;
        }

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

0개의 댓글