작년에 프로보노 공모전을 진행하면서 '스파르타 코딩클럽'에서
원하는 강의를 수강할 수 있는 쿠폰을 제공받았었다.
3개를 지급 받았는데, 2개는 공모전을 진행하면서 필요한 학습을 위해 사용하였고,
나머지 1개는 종강 후 조금 여유가 있을 때 공부하려고 남겨두었다.
고민 끝에, "웹 개발의 봄, Spring"강의를 선택하였다.
AWS를 사용해보면서, 서버를 직접 구현해보고 싶다는 생각이 있었는데,
이번 기회에 도움이 많이 되었으면 좋겠다
총 5주차의 과정에서 공부한 내용을 간단하게만 기록하려고 한다!
(나중에 내가 복습할 때도 편하도록..ㅎ)
1주차는 프로그램 설치, 환경 설정, 자바 기초문법, JSON구조, gradle 등에 대해 다루었다.
※이번 주차 기록에서 설치와 설정, java 문법은 기초적이라 생략했다
▶ 스프링 서버를 띄워 JSON 형태로 브라우저에 나타낼 때 깔끔하게 확인하기 위해 JSONView 설치
▶ Rest?
→ 서버의 형식이 JSON 형식임을 나타낸다.
→ html, css 등을 주고받을 때는 Rest를 붙이지 않는다.
▶ Controller?
→ 자동응답기 : 누군가 말을 걸면 응답한다.
→ 클라이언트의 요청(Request)을 전달받는 코드를 Controller라고 부른다.
JSON 만을 돌려주는 것을 RestController라고 부른다.
▶ RestControlloer 만들기
→ controller 패키지를 만든다.
@RestController
public class PersonController {
@GetMapping("/myinfo")
public Person getCourses() {
Person person = new Person();
person.setName("Isabelle");
person.setAge(23);
person.setJob("student");
return person;
}
→ @GetMapping : 브라우저에서 주소를 치는 행위를 GET 방식으로 정보를 요청한다고 한다. 스프링 주소(http://localhost:8080) 뒤의 주소가 /myinfo 일 경우, getCourse 메소드를 실행함을 나타낸다.
▶ 그레이들(Gradle)이란?
→ 코딩을 할 때에는 javascript - NPM, Python - pip, Java - mavenCentral,jcenter 와 같이 많은 도구(라이브러리)를 이용한다. 이때, 다운로드받고 적용하는 과정을 보다 편하게 하도록 위한 것이다.
→ build.gradle 파일의 dependencies 안에 작성한다.
★과제
Person.java
package com.sparta.week01;
public class Person {
private String name;
private int age;
private String job;
public Person(){}
public Person(String name, int age, String job){
this.name = name;
this.age= age;
this.job = job;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public void setJob(String job){
this.job = job;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String getJob(){
return this.job;
}
}
PersonController.java
package com.sparta.week01.controller;
import com.sparta.week01.Course;
import com.sparta.week01.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@GetMapping("/myinfo")
public Person getCourses() {
Person person = new Person();
person.setName("Isabelle");
person.setAge(23);
person.setJob("student");
return person;
}