문제점
springMvcApplication을 실행 중 에러가 발생
"status": 404,
"error": "Not Found",
"message": "No message available",
시도해본 것
다시 만들고 실행
구글링 해보니 404 에러가 종류가 많았음. 해당 호출을 찾을 수 없다는 문제인듯..
해결
별것도 아닌 문제 였다.
mvc파일의 위치가 문제였다. 해당 프로젝트 패키지 내에 위치해있지 않아 다른 templates나 static같은 값들을 읽어오지 못 한것.
위치를 해당 프로젝트 패키지로 옮겨줬더니 해결
알게된 것
위 문제로 알게된 것보다. spring을 새로 공부하며 아래에 알게된게 더 많은 듯 싶은데,
spring을 이용하는게 처음이라 일어난 헤프닝이었던것 같다.이것때문에 30분 넘게 끙끙댐...
그외 알게된것
개발자도구 network->header에서 볼 수 있는 http 에러 코드
100~ : 클라이언트가 서버와 연결상태를 확인하기위한 용도로 사용
200~ : http요청이 성공적으로 수행되었다.
300~ : 클라이언트가 추가적 조치를 취해야함을 나타냄. 페이지이동 혹은 redirection등에 사용됨.
400~ : 클라이언트의 잘못된 요청 혹은 인증오류 등에 나타남. 404는 우리가 요청한 브라우저나 페이지를 서버에서 찾을 수 없음을 나타냄
500~ : 서버에 오류가 발생했음을 나타냄.오류 또는 과부하 500은 서버내부에서 오류가 발생했음을 나타낸다.
Lombok 기능
@Getter : get 메서드들을 만들어줌
@Setter : set 메서드들을 만들어줌
@GetMapping : Get 통신
@PostMapping : Post 통신
@DeleteMapping : Delete 통신
@PutMapping : Put 통신
@Controller : 해당 클래스를 컨트롤러 클래스라고 알려준다.
@ResponseBody : 통신할 때 view반환이 아닌 데이터 반환이라고 알려줌
@RestController : Controller와 ResponseBody를 합친것. Controller에 ResponseBody도 자동으로 적용.
@AllArgsConstructor : 모든 필드를 받는 생성자를 만들어줌
@NoArgsConstructor : 필드를 받아오지 않는 즉 기본생성자를 생성해줌
@RequireArgsConstructor : final이 붙은 필드만 받아오는 생성자를 만들어줌
resource폴더내의 apllication.properties : spring과 관련된 설정을 할 수있다. 자동으로 설정되고있던 설정값을 수동으로 설정가능
MySQL이용하기
cd /usr/local/mysql/bin : 위치이동
./mysql -u root -p : MySQL접속 비밀번호 입력
SQL Error 2002 발생 sql 서버를 작동시키지 않아 에러가 발생한것 이었음
@Controller이용해서 html출력하기
만약 정적인 html을 출력할것이라면 resource폴더 -> static폴더에 html파일을 만들고 Controller를 거치지 않고 이름을 url에 바로 입력해서 접근할 수 있다.
json 형태와 String 형태 변환하여 반환하기
- json형을 String형으로 변환해줍니다.
objectMapper.writeValueAsString([json값]);
예시코드
@Test
@DisplayName("Object To JSON : get Method 필요")
void test1() throws JsonProcessingException {
Star star = new Star("Robbie", 95);
ObjectMapper objectMapper = new ObjectMapper(); // Jackson 라이브러리의 ObjectMapper
String json = objectMapper.writeValueAsString(star);
System.out.println("json = " + json);
}
- String 형을 json형으로 변환해줍니다.
objectMapper.readValue(문자열변수, Star.class);
// (문자열변수, 클래스타입.class)
예시코드
@Test
@DisplayName("JSON To Object : 기본 생성자 & (get OR set) Method 필요")
void test2() throws JsonProcessingException {
String json = "{\"name\":\"Robbie\",\"age\":95}"; // JSON 타입의 String
ObjectMapper objectMapper = new ObjectMapper(); // Jackson 라이브러리의 ObjectMapper
Star star = objectMapper.readValue(json, Star.class);
System.out.println("star.getName() = " + star.getName());
}
Gradle dependencies
implementation 'org.springframework.boot:spring-boot-starter-validation'
// JPA 설정
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// MySQL
implementation 'mysql:mysql-connector-java:8.0.28'
// JWT
compileOnly group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'
//Thymeleaf, springboot web, SpringSecurity,lombok, testcode
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'