[Spring Boot] 14. 디버깅 (Controller)

김건우's avatar
Apr 01, 2025
[Spring Boot] 14. 디버깅 (Controller)

1. this.storeService = storeService; 삭제시

문제 코드

public class StoreController { private StoreService storeService; public StoreController(StoreService storeService) { // this.storeService = storeService; (❌ 이 코드가 없음) } }

발생하는 오류

  • storeService가 초기화되지 않아서 null 상태가 됨.
  • storeService.상품목록() 같은 메서드를 호출할 때 NullPointerException 발생.

오류 메시지

java.lang.NullPointerException: Cannot invoke "StoreService.상품목록()" because "this.storeService" is null at com.example.demo.controller.StoreController.list(StoreController.java:14)

2. @Controller 삭제시

: Spring이 컨트롤러로 인식 못함

문제 코드

// @Controller (❌ 이 코드가 없음) public class StoreController { private StoreService storeService; public StoreController(StoreService storeService) { this.storeService = storeService; } }

발생하는 오류

  • Spring이 StoreController를 컨트롤러로 등록하지 않음.
  • /store/{id} 같은 요청을 처리하지 못하고 404 오류 발생.

오류 메시지

There was an unexpected error (type=Not Found, status=404). No mapping for GET /store/1

3. @GetMapping(”/”) 삭제시

: 메인 페이지 요청 불가능

문제 코드

// @GetMapping("/") (❌ 이 코드가 없음) public String list(HttpServletRequest request) { List<Store> storeList = storeService.상품목록(); request.setAttribute("models", storeList); return "store/list"; }

발생하는 오류

  • 브라우저에서 /로 접근하면 404 Not Found 발생.
  • Spring이 /에 대한 매핑이 없다고 판단.

오류 메시지

There was an unexpected error (type=Not Found, status=404). org.springframework.web.servlet.resource.NoResourceFoundException: No static resource .

4. @PathVariable(”id”) 삭제시

문제 코드

@GetMapping("/store/{id}") public String detail(int id, HttpServletRequest request) { //❌ @PathVariable 없음 Store store = storeService.상세보기(id); request.setAttribute("model", store); return "store/detail"; }

발생하는 오류

  • URL에서 {id} 값을 가져와야 하는데, @PathVariable이 없으면 Spring이 이를 바인딩하지 못함.
  • 400 Bad Request 또는 IllegalArgumentException 발생.

오류 메시지

Resolved [org.springframework.web.method.annotation. MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int']

5. return “redirect:/”; 삭제시

문제 코드

@PostMapping("/store/{id}/delete") public String delete(@PathVariable("id") Integer id) { storeService.상품삭제(id); // return "redirect:/"; (❌ 이 코드가 없음) }

발생하는 오류

  • return이 없으면 Spring이 기본적으로 뷰 이름이 null인 것으로 처리하여 오류 발생.
  • HTTP Status 500 – Internal Server Error 또는 응답 없음 발생.

오류 메시지

There was an unexpected error (type=Internal Server Error, status=500). Unresolved compilation problem: This method must return a result of type String

6. @RequsetParam 삭제시

: 요청 파라미터를 못 받음

문제 코드

@PostMapping("/store/save") public String save(String name, int stock, int price) { // ❌ @RequestParam 없음 storeService.상품등록(name, stock, price); return "redirect:/"; }

발생하는 오류

  • HTML <form>에서 name, stock, price 값을 보냈지만, @RequestParam이 없어서 Spring이 이를 바인딩하지 못함.
  • 400 Bad Request 오류 발생.

오류 메시지

Required request parameter 'name' is not present

Share article

gunwoo