다사다난 했던 디버깅 과정

개발세발·2024년 1월 25일

문제 발생

  • 여느 때와 다름 없이 강의를 들으며 실습을 하던 중...Spring Web MVC Framework를 구현해보는 단계에서 문제가 발생했습니다...

    바로 이 404에러를 말이죠...말도 그래서 뭐가 문제인지 천천히...살펴 보게 됐습니다..

해결 과정


  • 디버깅 과정 작성을 위해 기존 파일들을 깃허브에 업로드 해놓았습니다... 우선 Spring Web MVC에서 필요하지 않은 파일들이 너무 많은 것 같아서 다 삭제해주었습니다.

  • 이렇게 세분화되었던 컨트롤러들과 핸들러 매핑 파일들은 Spring을 사용하기 때문에 필요가 없어졌기 때문입니다.

  • DB와의 매핑은

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.repository.BookMapper">

    <select id="bookList" resultType="com.example.entity.BookDTO">
        select * from book order by title desc
    </select>

    <insert id="bookInsert" parameterType="com.example.entity.BookDTO">
        insert into book(title, price, name, page)
        values(#{title}, #{price}, #{name}, #{page})
    </insert>

    <!--
    <select id="userLogin"
            resultType="com.example.entityUserDTO"
            parameterType="com.example.entityUserDTO">
        select * from usertbl
        where username=#{username} and password=#{password}
    </select>
    -->
    <delete id="bookDelete" parameterType="int">
        delete from book where num=#{num}
    </delete>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
       http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- Root Context: defines shared resources visible to all other web components -->

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hellodb"/>
        <property name="username" value="root"/>
        <property name="password" value="0000"/>
    </bean>

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mybatis-config/mapper/BookMapper.xml" />
    </bean>

    <mybatis-spring:scan base-package="com.example.repository"/>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.example.controller" />

</beans:beans>
  • 요런 파일들로 관리를 해주고, sql(mysql 사용 중)문 역시 동 파일에서 관리하기 때문에 DAO라던지, 핸들러매핑 파일이라던지 여타 파일들이 필요가 없어져 삭제했습니다.

  • 또한 마지막 파일로 뷰와 연동해주었기 때문에 컨트롤러에서 직접적으로 뷰와의 경로를 노출시켜주지 않아도 됐습니다.

또 다른 문제

  • 그리고 실행 전에 Spring framework, gradle, 자바 버전이 서로 호환되지 않았습니다. 기존에 자바 20으로 계속 진행하고 있었는데, spring framework와, javax.servlet 요런 것들이 호환이 되지 않아 자바 17로 버전을 내려서 이 문제를 해결하였습니다.
    자바 버전 별 추가된 기능이나 쓸 수 있는 기능에 대해서 알아 놓으면 좋다라는 멘토님의 조언이 생각나는 순간이였습니다....

  • SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" 라는 문제도 발생했었는데, 전에 Spring Boot 강의에서 봤던 slf4j라는 로그백 어노테이션이 생각이 났고, 로그를 보여주도록 수정했습니다... 이렇게 말이죠..

implementation 'ch.qos.logback:logback-classic:1.2.3'

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
  • 의존성 추가 및 resources에 로그백xml을 추가하여 콘솔 창에서 로그를 보여주도록 했습니다.
  • 이후 디버깅 과정은 로그를 보면서 수정했던 것 같습니다.

느낀점...?

  • 일단 정상적으로 돌아가긴 해서,,,고쳤다! 라고 믿고 있지만...너무 얼렁뚱땅 한 기분이라서 찝찝한 느낌은 져버릴 수 없습니다...
    지속적인 공부가 필요할 것 같습니다...화이팅

0개의 댓글