Java

Java에서 서블릿을 둘 이상의 URL 패턴에 매핑하도록 web.xml을 구성하는 방법 (No mapping found for HTTP request with URI 오류가 발생한다면?)

URL 패턴을 두가지 방향으로 처리해야하는 상황에 직면하였다.

현재는 .do URL만 접근 허용토록 하고 있다.

[web.xml]

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/springmvc/dispatcher-servlet.xml,
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<!--
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
-->

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

 

다음은 여러 URL을 동일한 서블릿에 매핑하는 샘플 web.xml 의 내용이다.

/test 로 접근하는 모든URL를 체크하기위해 URL 패턴을 추가하였다.

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    <url-pattern>/test/*</url-pattern>
</servlet-mapping>

하지만 예상대로 적용되지않고 No mapping found for HTTP request with URI 로그를 직면하였다.

2022-05-06 10:10:51.315 [http-bio-3000-exec-4] 
WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/test/whoAreU] in DispatcherServlet with name 'action'
 
오류의 발생 원인을 알기 위해서는 서블릿 컨테이너와 웹 어플리케이션간의 연동 방법을 알아야 하는데 url-pattern에 등록할 수 있는 URL은 다음과 같다.
  • “*.”으로 시작하는 패턴의 경우 확장자만 매칭하여 처리
  • “/”로 시작하고 “/*”로 끝나는 패턴은 path로 인식
  • “/”만 정의한 경우 디폴트 서블릿 : 디폴트 서블릿은 서블릿 매핑 URL에 걸리지 않는 요청들을 처리 (DefaultServlet은 png, jpg, js, html등 정적인 content를 처리한다는 의미)
http://localhost:8080/test/userList.jsp <- JspServlet 요청 처리

http://localhost:8080/img/logo.jpg <- DefaultServlet 요청 처리
http://localhost:8080/test/list <- DispatcherServlet 요청 처리

DispatcherServlet은 url-pattern을 “/” 와 같이 설정하게 되면서 tomcat의 server.xml에 정의되어 있는 url-pattern “/”을 무시한다. 

결국 DispatcherServlet url-pattern을 재정의하게 되어서 DefaultServlet은 더이상 동작할 수 없게 된 것이다.

 
스프링에서는 이를 해결하기 위해서 <mvc:default-servlet-handler /> 설정을 지원한다.
 

 

<mvc:annotation-driven> 애노테이션 방식의 @MVC를 사용시 필요한 몇가지 빈들을 자동으로 등록해 준다.

자동으로 등록되는 빈이 어떤것인지 기억해두고 이를 다시 <bean>태그로 등록하지 않게 해야 한다.

AnnotationMethodHandlerAdapter와 DefaultAnnotationHandlerMapping등의 설정을 변경해야 할 때는 

<mvc:annotation-driven>을 사용할 수는 없고 이때는 직접 필요한 빈을 등록하고 프로퍼티를 설정해줘야 한다.

 

 

[오류 해결을 위한 처리 방안]

“No mapping found for HTTP request with URI” 해당 오류는 일반적으로 URL을 처리할 컨트롤러를 찾지 못할 때 발생하는 오류이다. 문제의 가능성에 대해 아래에 점검사항을 확인!!


1. XXXController에서 @controller가 선언이 되어있는지 확인

2. web.xml에서 servlet-mapping 내의 url-pattern의 경로를 점검
3. was설정의 context root path설정을 점검

 

<servlet>
    <servlet-name>domain</servlet-name>
    <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/springmvc/domain-servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>domain</servlet-name>
    <url-pattern>/test/*</url-pattern>
</servlet-mapping>

  

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>
    
    <context:component-scan base-package="com.test.login.*" use-default-filters="false" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

    
</beans>
 
오류가 해결되지 않는 경우,
다음과 같이 /*로 처리하고, dispacher-servlet.xml에 <mvc:default-servlet-handler/>를 추가해주면 해결 될 수 있다. 
<?xml version="1.0" encoding="UTF-8"?>
<!-- DispatcherServlet application context for Application's web tier. -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<mvc:default-servlet-handler/>
    
    .....생략........
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
 

[REFERENCE]

 

 

Leave a Reply

error: Content is protected !!