개발 환경 설정

황준하·2023년 6월 15일

Spring 기본

목록 보기
1/38
post-thumbnail

1. main과 test가 구분되어있다.

\= test code가 중요하다.

2. tomcat이 내장되어 있어서 실행하면 바로 포트 8080으로 서버가 실행된다.

3. 스프링부트 스타터 : 스프링부트 + 스프링 코어 + 로깅

4. 테스트 라이브러리

mockito

assertj

spring-test

5. spring.io를 활용해서 찾는것이 중요하다.

6. 컨트롤러에서 리턴값으로 문자를 반환하면 뷰 리졸버가 화면을 찾아서 처리한다.

스프링 부트 템플릿 엔진 기본 viewName 매핑

'resources:templates/' + {ViewName} + '.html'


Spring 기초

정적컨텐츠

MVC(model view controller) 와 템플릿 엔진(jsp, php와같이 동적으로 서버에서 변향해주는 것)

API(서버끼리 통신할때)

정적 컨텐츠

@GetMapping("hello") //localhost/hello
    public String hello(Model model){
        model.addAttribute("data", "hello!!");
        return "hello";     //templates/hello.html 템플릿 엔진 처리
    }

https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.static-content

 [Web

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io](https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.static-content)

hello-static 관련 컨트롤러가 없다.

-> /static 에서 정적 컨텐츠를 찾아 불러온다.

컨트롤러가 우선순위가 높다.

MVC와 템플릿 엔진

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

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

model(name:spring)

파라미터를 받아서 templates/hello-template.html (Thymeleaf 템플릿 엔진이 처리해준다)

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

@ResponseBody 로 들어오면 hello(name:spring) 객체를 넘겨준다.

viewResolver 대신에 HttpMessageConverter가 동작한다.

(JsonConverter or StringConverter)

기본 객체 처리 : 'MappingJackson2HttpMessageConverter'

0개의 댓글