Spring Boot 오류 해결

HyeonJeong·2023년 1월 3일
0

오류를 해결하자

목록 보기
2/2

해당 내용은 spring 프로젝트를 진행하면서 제가 만난 오류 코드이며, 해당 내용에 대해서 원인과 해결방안을 작성해 보았습니다.

📌Spring 기초 오류


8080 포트로 미동작 (APPLICATION FAILED TO START)

  • 원인
    • 이미 사용 중(Web server failed to start. Port 8080 was already in use.)

  • 이유
    • 프로그램 실행 전 미처 확인하지 못함

  • 해결
    1. cmd에 사용중인 port 리스트 출력(netstat -a -o)
    2. 해당 PID 죽이기

Whitelabel Error Page

  • 문제
    • lombok 미연결
  • 해결
    • @controller 위에 @ResponseBody 추가로 해결
      • @RestController = (@Controller + @ResponseBody) 조합

        → RESTful 웹 서비스를 보다 쉽게 개발할 수 있도록

      • controller 역할 : model 객체 만들어 데이터 담고, view 찾는 것

        ↔ @RestController : 객체만 반환, 객체 데이터는 JSON/XML 형식으로 HTTP 응답에 담음

      • 차이 : @RestController을 표시하면 모든 메소드가 뷰 대신 객체로 작성

      • 참고자료

        @Controller와 @RestController의 차이점


DefaultHandlerExceptionResolver

  • MissingServletRequestParameterException

    • 문제
      • Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]

    • 해결
      - 파라미터가 필요한 controller 동작에 해당 파라미터를 넣지 않고 돌림

    • 참고자료

      https://gongbuzzangzoa.tistory.com/326


APPLICATION FAILED TO START

  • 문제

    • The bean 'postService', defined in class path resource [hello/hellospring/SpringConfig.class], could not be registered. A bean with that name has already been defined in file [C:\Users\1212g\Downloads\hello-spring\out\production\classes\hello\hellospring\service\PostService.class] and overriding is disabled.
  • 해결

    • 빈 등록을 중복하여 발생하는 문제
    • @Service어노테이션을 삭제(주석)처리 or SpringConfig.class에서 Service등록(@Bean)을 제거

servlet

  • 문제
    • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  • 해결
    • private으로 선언만 하는 부분에 final까지 추가

hibernate

  • 문제
    • detached entity passed to persist: hello.hellospring.domain.Post

  • 해결
    • 엔티티 클래스에 @Id를 부여한 필드에 @GeneratedValue를 작성하여 AUTO, SEQUENCE, IDENTITY 전략 등 데이터베이스에게 key 값을 자동 생성하도록 하는 전략을 선택했으나, 추가로 ID 값을 만들어서 DB에 전송한 경우

      → 해당 추가 ID 넣는 부분 제거


0개의 댓글