일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- mustache
- TDD
- Comparable
- 프로그래머스
- rombok
- testcode
- DynamicProgramming
- spring
- SpringDataJPA
- SpringFramework
- DB
- baekjoon
- tdo
- API
- 알고리즘
- Java
- kotlin
- JPA
- C++
- SpringBean
- JPA autiding
- SpringSecurity
- 코딩테스트
- springboot
- gitignore
- test case
- index.html
- Gradle
- 해싱
- 알고리즘 #코딩테스트 #프로그래머스 #C++ #vector #string
- Today
- Total
천천히, 한결같이
[Spring] Mustache로 기본 페이지(index.html) 만들기 본문
이동욱 님의 스프링 부트와 AWS로 혼자 구현하는 웹 서비스책을 공부하며 정리한 내용입니다. 틀린 정보가 있을 수 있으니 주의하시고 댓글로 남겨주시길 바랍니다.
기본 페이지 만들기
항상 스프링의 프로젝트에서 새로운 DB, 언어 등을 사용하기 위해선 항상 의존성을 추가해야 합니다! 이전에 했던 방법대로, build.gradle에 아래와 같이 입력해서 Mustache의 의존성을 등록합니다.
compile('org.springframework.boot:spring-boot-starter-mustache')
의존성을 추가했다면 항상 refresh를 통해서 관련된 설정을 받아옵니다! IntelliJ를 기준으로 dependencies를 새롭게 추가했다면 우측 상단에 나오는 코끼리 아이콘을 클릭해서 의존성을 추가합니다.
머스테치의 파일 위치는 기본적으로 src/main/resources/templates
입니다. 이 위치에 mustache 파일을 두면 스프링 부트에서 자동으로 로딩합니다. 첫 페이지를 담당할 index.mustache
를 위의 디렉토리에 생성합니다. index.mustache
의 코드는 아래와 같습니다!
- index.mustache
<!DOCTYPE HTML>
<html>
<head>
<title>SpringBoot Webservice</title>
<meta http-equiv="Content-Type" content = "text/html"; charset="UTF-8" />
</head>
<body>
<h1>Webservice with SpringBoot</h1>
</body>
</html>
이제 위 html파일을 보여주는 url을 매핑해야 합니다! 당연하게 index파일을 보여주는 url을 매핑해야 되니까, IndexController
를 새로 만들어줍니다.
머스테치 스타터 덕분에 컨트롤러에서 문자열을 반환할 때 앞의 경로와 뒤의 파일 확장자는 자동으로 지정됩니다. 앞의 경로는 src/main/resources/templates
로, 뒤의 파일 확장자는 .mustache
가 붙는 것입니다. 즉 여기에선 index
를 반환하므로, src/main/resources/templates/index.mustache
로 변환되어서 View Resolver가 처리하게 됩니다.
이제 잘 적용이 되었는지, 테스트 코드를 통해서 검증해 보겠습니다!
- IndexControllerTest
package com.hwanld.book.springboot.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment =RANDOM_PORT)
public class IndexControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void 메인페이지_로딩() {
//when
String body = this.restTemplate.getForObject("/", String.class);
//given
//then
assertThat(body).contains("Webservice with SpringBoot");
}
}
TestRestTemplate를 통해 "/"로 호출했을 때 index.mustache
에 포함된 코드들이 있는지만 확인하면 됩니다. 전체 코드를 다 검증할 필요는 없으니, 위 테스트 코드와 같이 "Webservice with SpringBoot" 문자열이 포함되어 있는지만 확인하면 됩니다.
실제로 잘 작동하는지 눈으로 확인하기 위해서, Tomcat을 실행한 후 http:/localhost:8080
으로 접속해서 확인해 보겠습니다.
다음과 같이 잘 작동되는 것을 확인할 수 있습니다!
'Spring' 카테고리의 다른 글
[Spring] 스프링 시큐리티, OAuth2를 사용해서 Google 서비스 등록하기 (0) | 2022.01.08 |
---|---|
[Spring] 글 등록 화면 만들기 (0) | 2022.01.02 |
[Spring] 의존성 주입(Dependency Injection) (0) | 2021.12.31 |
[Spring] JPA Auditing으로 생성시간, 수정시간 자동화하기 (0) | 2021.12.31 |
[Spring] API 생성하기(2) (0) | 2021.12.30 |