본문 바로가기

Backend/Spring

[SpringBoot] 블로그 프로젝트 #0 Github 연동하기

본 포스팅은

https://chanho0912.tistory.com/18

 

[SpringBoot] SpringBoot와 Mysql 연동

기본적인 세팅을 완료했으면 Mysql연동을 진행해 볼게요. https://chanho0912.tistory.com/4 [Spring/SpringBoot] IntelliJ 소개 및 Gradle 프로젝트 설정 필자는 SpringBoot 개발 환경으로 보통 IntelliJ를 사용..

chanho0912.tistory.com

 

해당 포스팅까지 완료하여 Mysql 설정까지 완료되었다고 가정하고 블로그 프로젝트 포스팅을 시작하겠습니다.

 

우선 간단히 저희가 빌드한 Spring Application이 정상적으로 작동하는지 확인해 보겠습니다.

 

 

우선 다음과 같이 저희의 메인 Application class와 간단하게 테스트해볼 HelloController class를 추가해 주겠습니다.

 

package com.BlogWithSpringBoot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BlogApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
}

< BlogApplication class >

 

package com.BlogWithSpringBoot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class HelloController {

    @GetMapping("/hello")
    @ResponseBody
    public String return_hello () {

        return "hello world !!";
    }
}

< HelloController class >

 

"hello world !!"라는 간단한 String을 반환해주는 Controller입니다. @Controller 대신 @RestController를 사용하고 @ResponseBody를 지워도 같은 작동을 합니다. 

 

* @ResponseBody는 Data를 json형태로 받아오는 어노테이션입니다.

* Spring 4.0버전 이후부터는 @RestController 어노테이션을 붙이면 위와 같은 역할을 만들어 더 코드를 Restful 하게 작성할 수 있습니다.

 

작동이 확인되는 것만 볼것이기 때문에 이 경우에는 어느 쪽이든 상관없습니다.

 

package com.BlogWithSpringBoot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void TestHello () throws Exception {
        String hello = "hello world !!";

        mockMvc.perform(get("/hello"))
                .andExpect(status().isOk())
                .andExpect(content().string(hello));
    }

}

< HelloControllerTest class >

HelloControllerTest의 코드입니다. 

 

기본적인 Test 모듈인 MockMvc를 주입받아 get method를 실행한 뒤, 정상적으로 HttpStatus가 200이 되는지 확인합니다. 또한 ResponseBody의 Content가 저희가 반환한 "hello world!!"와 맞는지 확인합니다.

 

test 성공 화면

 

실제로 localhost:8080/hello에서도 확인해 볼 수 있습니다.

 

 

자 그러면 마지막으로 Github과 연동하여 프로젝트 사전 준비를 마무리하겠습니다.

 

저 같은 경우는 이미 Github계정과 IntelliJ가 연동되어 있기 때문에 좌측 상단 Tool에 Git->Github->SharedProject on Github을 선택할 경우 자동으로 Repository가 생성되며 연동이 됩니다. 

 

하지만 처음 연동하시는 분이라면, 해당 화면을 클릭하였을 때 Log in to Github이라는 간단한 창이 뜰 텐데, 거기서 본인 계정으로 로그인해주시면 됩니다.

 

 

그러면 아마 현재 생성된 모든 Directory가 보이며 Commit을 진행할 것인지 확인하는 창이 보입니다.

 

거기서. idea 파일을 제외하고 Commit, Push를 진행해주시면 됩니다.

*. idea 파일은 인텔리제이에서 실행하기 위한 파일이기 때문에 Github에 올릴 필요가 없습니다.

 

 

여기까지 완료되었으면 

 

윈도 기준 ctr + shift + a를 입력하셔서 plugins를 검색하시고

 

 

해당 검색에 .ignore를 검색하셔서 Apply를 눌러주시면 됩니다.

 

인텔리제이에서는 Plugin 설치가 완료되면 재시작을 해야 합니다. 재시작해주시면 됩니다.

 

그러면 다음과 같이 .ignore 파일이 생성된 것을 볼 수 있습니다.

 

build와 관련된 파일들과 .gradle, .idea를 적어주시면 됩니다.

 

자 그러면 이제 Terminal을 열어서 push를 해봅니다.

 

 

git init

git add .

git commit -m "commit 내용"

 

까지 진행해 주시고 성공적으로 Commit 버전이 생성되면

 

git push -u origin +master

다음의 명령어로 Push 해줍니다.

 

여기까지 완료되었으면 

 

Github Repository에서 git ignore까지 commit이 완료된 것을 확인해 주시면 됩니다.

 

*저의 글에 대한 피드백이나 지적은 언제나 환영합니다.

'Backend > Spring' 카테고리의 다른 글

[SpringBoot] 블로그 프로젝트 #1 JSP 설정  (0) 2021.06.27
[Spring] Spring, Postman을 통해 Http통신 테스트  (0) 2021.06.25
[SpringBoot] SpringBoot와 Mysql 연동  (0) 2021.06.24
[Spring] PSA란?  (0) 2021.06.24
[Spring] AOP란?  (1) 2021.06.23