Skip to content

Green-Cloud-Project/GreenCloudServer

Repository files navigation

SpringBootPractice

μŠ€ν”„λ§λΆ€νŠΈ μ—°μŠ΅

[TOC]

Table of Contents

1. 아무것도 μ—†λŠ” μŠ€ν”„λ§ λΆ€νŠΈ ν”„λ‘œμ νŠΈ 생성
2. Coltroller μƒμ„±ν•˜κΈ°
3. λ§ˆμ΄λ°”ν‹°μŠ€ μŠ€ν”„λ§ λΆ€νŠΈ ν”„λ‘œμ νŠΈ 생성

μƒˆν”„λ‘œμž­νŠΈ μ„€μ •

  • ArtifactλŠ” λŒ€λ¬ΈμžλŠ” 넣을 수 μ—†λ‹€.
  • Type에 Gradle Projectλ₯Ό μ„ νƒν•œλ‹€.
  • λ‹€μŒλ‹¨κ³„λ‘œ μ΄λ™ν•œλ‹€.

Dependencies μ„€μ •

  • Dependenciesλ₯Ό μ„ νƒν•˜λŠ” 뢀뢄이닀.
  • ν”„λ‘œμž­νŠΈλ₯Ό 이미 μƒμ„±ν•œ ν›„ 이뢀뢄을 λ‹€μ‹œ λ“€μ–΄κ°€κ³  싢은데 μ§„μž…λ°©λ²•μ„ λͺ¨λ₯΄κ² μŒ. γ… γ… 
  • κ·Έλž˜μ„œ ν˜Ήμ‹œ 이뢀뢄을 λ‹€μ‹œ μˆ˜μ •ν•˜κ³  μ‹Άμ„λ•ŒλŠ” ν”„λ‘œμž­νŠΈλ₯Ό μƒˆλ‘œ μƒμ„±ν•œ ν›„ build.gradleνŒŒμΌμ—μ„œ dependencies 뢀뢄을 κ°€μ Έμ˜€λ„λ‘ ν•˜μž
  • λ‹€μŒλ‹¨κ³„λ‘œ μ΄λ™ν•œλ‹€

ν”„λ‘œμž­νŠΈ μ„€μ • 마무리

  • ν”„λ‘œμž­νŠΈλ₯Ό μ €μž₯ν•  μ›ν•˜λŠ” 경둜λ₯Ό 선택 ν›„ Finishλ₯Ό μ„ νƒν•œλ‹€.

기타 μ„€μ • νŒμ—…

  • ν”„λ‘œμž­νŠΈκ°€ μƒμ„±λ˜κ³  μ•„λž˜μ™€ 같은 νŒμ—…μ΄ λœ¬λ‹€.
  • 잘λͺ¨λ₯΄κ² μ–΄μ„œ κ·Έλƒ₯ OK

ν”„λ‘œμ νŠΈ λΉŒλ“œ

  • ν”„λ‘œμž­νŠΈ 생성 ν›„ λΉŒλ“œλ₯Ό ν•œλ‹€. μ•„λž˜μ™€ 같은 κ²°κ³Όκ°€ λ‚˜μ˜¨λ‹€.

컨트둀러 μƒμ„±ν•˜κΈ°

ν˜„μž¬ μ›ν•˜λŠ” λͺ©ν‘œλŠ” APIμž‘μ—…μ„ ν•˜λŠ”κ±΄λ°, ν΄λΌμ΄μ–ΈνŠΈ λ‹¨μ—μ„œ url을 ν˜ΈμΆœν•˜λ©΄ μ›Ήμ„œλ²„μ—μ„œ ν•΄λ‹Ήν•˜λŠ” μš”μ²­μ„ μ²˜λ¦¬ν•΄μ€˜μ•Ό ν•˜λŠ” μž‘μ—…μ„ ν•œλ‹€. Controller은 μ΄λŸ¬ν•œ μš”μ²­μ— λŒ€ν•œ μ²˜λ¦¬ν•˜λŠ” 뢀뢄을 λͺ…μΉ­ν•˜λŠ” 것 κ°™μœΌλ©°, μŠ€ν”„λ§λΆ€νŠΈμͺ½μ—μ„œ @controllerμ΄λž€ μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬μ€˜μ•Ό 이게 λ™μž‘ν•˜λŠ” ꡬ쑰둜 λ˜μ–΄μžˆμ–΄ λ³΄μž…λ‹ˆλ‹€.(λ‡Œν”Όμ…œ 죄솑)

  • μ»¨νŠΈλ‘€λŸ¬μ—λŠ” @Controllerμ΄λž€ μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬μ•Όν•œλ‹€.
  • μ•ˆκ·Έλ €λ©΄ RequestMapping 후에 url μš”μ²­ μ‹œ λ™μž‘ν•˜μ§€ μ•ŠμŒ.

RequestMapping μΆ”κ°€ν•˜κΈ°

이 μž‘μ—…μ„ ν•΄μ£Όλ©΄ λ“œλ””μ–΄ μ›Ήμ—μ„œ ν˜ΈμΆœμ„ ν•  수 μžˆκ²Œλœλ‹€.

  • ν•¨μˆ˜λ₯Ό 생성 ν›„ @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬μ€€λ‹€.
  • 라이브러리λ₯Ό μΆ”κ°€ν•΄μ€˜μ•Όν•œλ‹€.

Add spring-boot-starter-web

  • 이 ν•΄λ‹Ή 라이브러리λ₯Ό μΆ”κ°€ν•˜λŠ”λ•ŒλΆ€ν„° λΉŒλ“œ μ‹œ μ›Ήμ„œλ²„κ°€ μž‘λ™ν•˜λŠ” 것 같아보인닀.

λΉŒλ“œν•˜κΈ°

λΉŒλ“œκ²°κ³Ό

  • ν•¨μˆ˜μ— @ResponseBodyλ₯Ό μ²˜λ¦¬ν•΄μ€˜μ•Όν•œλ‹€.

ResponseBody μΆ”κ°€

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ μ‚¬μ΄νŠΈ μ°Έμ‘°

dependencies 에 λ§ˆμ΄λ°”ν‹°μŠ€λ₯Ό μΆ”κ°€

dependencies {
    //λ§ˆμ΄λ°”ν‹°μŠ€ μΆ”κ°€
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencies μΆ”κ°€λ§Œ ν•˜κ³  λΉŒλ“œμ‹œ μ•„λž˜μ™€ 같은 μ—λŸ¬κ°€ λ‚˜μ˜΄

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

application.properties에 μ•„λž˜μ™€ 같이 접속 정보λ₯Ό μ„€μ •ν•΄μ€˜μ•Όν•œλ‹€.

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://sarang628.iptime.org:3306/TravelBuddy?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=travelbuddy
spring.datasource.password=travelbuddy!@#

λͺ¨λΈμƒμ„±

public class User {
    int user_id;
    String name;

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}

맡퍼 생성

@Mapper
public interface UserMapper {

    @Select("Select * from User")
    ArrayList<User> getAllUsers();
}

POST νŒŒλΌλ―Έν„° λ°›κΈ°

@PostMapping("/join")
    public @ResponseBody
    String join(@RequestParam("token") String token) {
        return token;
    }

μŠ€ν”„λ§ λΆ€νŠΈ μš°λΆ„νˆ¬ λΉŒλ“œ

μš°λΆ„νˆ¬ λΉŒλ“œ ν™˜κ²½μ„ κ΅¬μΆ•ν•˜λŠ”λ° 4~5일정도 μ‚½μ§ˆμ„ ν•œ 것 κ°™λ‹€. ꡉμž₯히 κ³ λ‹¨ν•œ μ‹œκ°„μ΄μ˜€λ‹€γ… γ…  κ·Έλž˜λ“€ λΉŒλ“œν•˜λŠ”λ° μžλ°” 버전 λ§žμΆ”λŠ”κ²Œ μ€‘μš”ν–ˆμ—ˆλ‹€. 9버전 μ΄μƒμœΌλ‘œ κ°€λ‹ˆκΉŒ 가상 머신이 μ•ˆλœ(?)λ‹€λŠ” 였λ₯˜λ₯Ό λ±‰μ–΄μ„œ κ·Έ 외에 μ—¬λŸ¬κ°€μ§€ 였λ₯˜λ“€μ΄ μžˆμ—ˆλŠ”λ° 고치기 λ°”λΉ  기둝을 ν•  수 μ—†μ—ˆλ‹€. μ€‘μš”ν•œκ±΄ μžλ°” 8버전을 잘 μ„€μΉ˜ν•΄μ„œ μ‹€ν–‰ν•˜λ©΄ λ˜λŠ” 것 μ΄μ˜€λ‹€.

oracle-java8-installer νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.  ν•˜μ§€λ§Œ λ‹€λ₯Έ νŒ¨ν‚€μ§€κ°€ μ°Έμ‘°ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
ν•΄λ‹Ή νŒ¨ν‚€μ§€κ°€ λˆ„λ½λ˜μ—ˆκ±°λ‚˜ μ§€μ›Œμ‘Œλ‹€λŠ” λœ»μž…λ‹ˆλ‹€. μ•„λ‹ˆλ©΄ 또 λ‹€λ₯Έ κ³³μ—μ„œ
νŒ¨ν‚€μ§€λ₯Ό 받아와야 ν•˜λŠ” 경우일 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

E: 'oracle-java8-installer' νŒ¨ν‚€μ§€λŠ” μ„€μΉ˜ν•  수 μžˆλŠ” 후보가 μ—†μŠ΅λ‹ˆλ‹€

sudo add-apt-repository ppa:webupd8team/java
νŒ¨ν‚€μ§€ python-software-propertiesλŠ” 이미 μ΅œμ‹  λ²„μ „μž…λ‹ˆλ‹€ (0.96.20.8).
λ‹€μŒ νŒ¨ν‚€μ§€κ°€ μžλ™μœΌλ‘œ μ„€μΉ˜λ˜μ—ˆμ§€λ§Œ 더 이상 ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€:
  ant ant-optional ca-certificates-java default-jre-headless fonts-dejavu-extra java-common java-wrappers junit libactivation-java libantlr-java libaopalliance-java libapache-pom-java libasm-java
  libasm4-java libaspectj-java libass5 libatinject-jsr330-api-java libatk-wrapper-java libatk-wrapper-java-jni libavcodec-ffmpeg-extra56 libavdevice-ffmpeg56 libavfilter-ffmpeg5 libavformat-ffmpeg56
  libavresample-ffmpeg2 libavutil-ffmpeg54 libbcel-java libbcpg-java libbcprov-java libblas-common libbluray1 libbonobo2-0 libbonobo2-common libboost-program-options1.58.0 libboost-thread1.58.0
  libbsf-java libbsh-java libbz2-dev libcapnp-0.5.3 libcdi-api-java libcglib-java libcglib3-java libchromaprint0 libclassworlds-java libcommons-cli-java libcommons-codec-java
  libcommons-collections3-java libcommons-compress-java libcommons-dbcp-java libcommons-httpclient-java libcommons-io-java libcommons-lang-java libcommons-lang3-java libcommons-logging-java
  libcommons-net-java libcommons-net2-java libcommons-parent-java libcommons-pool-java libdom4j-java libdoxia-core-java libecj-java libeclipse-aether-java libeclipse-jdt-core-java libel-api-java
  libexpat1-dev libfelix-osgi-obr-java libgeronimo-annotation-1.3-spec-java libgeronimo-ejb-3.2-spec-java libgeronimo-interceptor-3.0-spec-java libgeronimo-jms-1.1-spec-java
  libgeronimo-jta-1.1-spec-java libgeronimo-validation-1.0-spec-java libgif7 libgnome-2-0 libgnome2-common libgnomevfs2-0 libgnomevfs2-common libgoogle-gson-java libgoogle-perftools4 libgsoap8
  libgtkglext1 libguava-java libguice-java libhamcrest-java libhardware2 libhawtjni-runtime-java libhttpclient-java libhttpcore-java libhybris libhybris-common1 libjansi-java libjansi-native-java
  libjarjar-java libjatl-java libjaxb-api-java libjaxen-java libjaxp1.3-java libjcifs-java libjcip-annotations-java libjcommander-java libjcsp-java libjdepend-java libjdom1-java libjdom2-java
  libjformatstring-java libjline2-java libjna-java libjna-jni libjsch-java libjsoup-java libjsp-api-java libjsr166y-java libjsr305-java libjzlib-java libkryo-java libkxml2-java libllvm4.0 libllvm5.0
  liblog4j1.2-java liblogback-java liblua5.1-0 libmad0 libmail-java libmaven-archiver-java libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java libmaven2-core-java libmaven3-core-java
  libmedia1 libmimic0 libminlog-java libnative-platform-java libnative-platform-jni libncurses5-dev libnekohtml-java libnetty-3.9-java libobjenesis-java libopencv-calib3d2.4v5 libopencv-contrib2.4v5
  libopencv-core2.4v5 libopencv-features2d2.4v5 libopencv-flann2.4v5 libopencv-highgui2.4v5 libopencv-imgproc2.4v5 libopencv-legacy2.4v5 libopencv-ml2.4v5 libopencv-objdetect2.4v5 libopencv-video2.4v5
  libopenjpeg5 liborbit-2-0 libosgi-annotation-java libosgi-core-java libosgi-foundation-ee-java libpcrecpp0v5 libplexus-ant-factory-java libplexus-archiver-java libplexus-bsh-factory-java
  libplexus-cipher-java libplexus-classworlds-java libplexus-classworlds2-java libplexus-cli-java libplexus-component-annotations-java libplexus-component-metadata-java libplexus-container-default-java
  libplexus-container-default1.5-java libplexus-containers1.5-java libplexus-interactivity-api-java libplexus-interpolation-java libplexus-io-java libplexus-sec-dispatcher-java libplexus-utils-java
  libplexus-utils2-java libpolyglot-maven-java libpostproc-ffmpeg53 libqdox-java libqdox2-java libreflectasm-java librhino-java librpm3 librpmbuild3 librpmio3 librpmsign3 libschroedinger-1.0-0
  libservlet-api-java libservlet3.0-java libservlet3.1-java libsisu-inject-java libsisu-plexus-java libslf4j-java libsnappy-java libsnappy-jni libsodium18 libspring-aop-java libspring-beans-java
  libspring-context-java libspring-core-java libspring-expression-java libspring-instrument-java libsrtp0 libswresample-ffmpeg1 libswscale-ffmpeg3 libtbb2 libtcmalloc-minimal4 libtext-unidecode-perl
  libtinfo-dev libtomcat7-java libtomcat8-java libtomcat9-java libv8-3.14.5 libva1 libwagon-file-java libwagon-ftp-java libwagon-http-java libwagon-http-shaded-java libwagon-java
  libwagon-provider-api-java libwebpdemux1 libwebsocket-api-java libwildmidi1 libx264-148 libx265-79 libxalan2-java libxbean-java libxbean-reflect-java libxerces2-java libxml-commons-external-java
  libxml-commons-resolver1.1-java libxom-java libxpp2-java libxpp3-java libxstream-java libxz-java libyaml-cpp0.5v5 libyaml-snake-java lua-lpeg m4 openjdk-11-jre-headless oxideqt-codecs-extra
  qml-module-qtwebkit ruby2.3 snapd-login-service testng tex-common texinfo xserver-xorg-input-evdev-hwe-16.04 xserver-xorg-input-synaptics-hwe-16.04 xserver-xorg-legacy-hwe-16.04
  xserver-xorg-video-amdgpu-hwe-16.04 xserver-xorg-video-qxl-hwe-16.04 xserver-xorg-video-vesa-hwe-16.04
Use 'sudo apt autoremove' to remove them.
java

Command 'java' not found, but can be installed with:

sudo apt install default-jre            
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless 

ssh 둜그인 없이 μ ‘μ†ν•˜κΈ°

  1. rsa ν‚€ μƒμ„±ν•˜κΈ°
ssh-keygen -t rsa
  1. .ssh/id_rsa.pub μ•ˆμ— μžˆλŠ” 값을 볡사.

  2. 원격 μ„œλ²„μ— 접속 ν›„ .ssh/authorized_keys(μ—†μœΌλ©΄ 생성) μ•ˆμ— λ³΅μ‚¬ν•œ 값을 λ„£μœΌλ©΄λ¨

  • 이미 λ“±λ‘λœκ²Œ μžˆλŠ”λ° κ·Έλƒ₯ μ—”ν„°μΉœλ‹€μŒ λ‹€μŒ 라인에 λΆ™μ—¬λ„£μœΌλ‹ˆκΉŒ 잘 λ™μž‘λ¬λ‹€.

DB μƒμ„±ν•˜κΈ°

MariaDB [(none)]> create database green_cloud
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user "green_cloud"@"%" identified by "green_cloud!@#"
    -> ;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on green_cloud.* to "green_cloud"@"%";
Query OK, 0 rows affected (0.00 sec)

Releases

No releases published

Packages

No packages published

Languages