Exception Handling in Spring MVC


http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc


시간날때 적용 해봐야겠다.

J2EE Servlet 명세에서 인코딩된(content type : application/x-www-form-urlencoded) HTTP PUT 메소드는 지원하지 않기 때문에 폼에 값을 담은 후 PUT으로 서버에 전송할 경우 Command Object 바인딩이 자동으로 되지 않는다.


해결책은 아래와 같다. 


자료 찾느라 열라 고생함 ㅠㅠ 

(아직 spring 기반의 RESTful 서비스가 많지 않아서 그런가? 아님 너무 당연해서 나만 몰랐던 건가?)


출처 : http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

16.3.3.11 Working with "application/x-www-form-urlencoded" data

The previous sections covered use of @ModelAttribute to support form submission requests from browser clients. The same annotation is recommended for use with requests from non-browser clients as well. However there is one notable difference when it comes to working with HTTP PUT requests. Browsers can submit form data via HTTP GET or HTTP POST. Non-browser clients can also submit forms viaHTTP PUT. This presents a challenge because the Servlet specification requires the ServletRequest.getParameter*() family of methods to support form field access only for HTTP POST, not for HTTP PUT.

To support HTTP PUT requests, the spring-web module provides the filter HttpPutFormContentFilter, which can be configured in web.xml:

<filter>
  <filter-name>httpPutFormFilter</filter-name>
  <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>httpPutFormFilter</filter-name>
  <servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

The above filter intercepts HTTP PUT requests with content type application/x-www-form-urlencoded, reads the form data from the body of the request, and wraps the ServletRequest in order to make the form data available through the ServletRequest.getParameter*() family of methods.


참고 : http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/filter/HttpPutFormContentFilter.html





아래의 코드에 따라 ....

@RequestMapping(value="/club/{seq}")

public String clubFrame(@PathVariable("seq") Integer seq) {

    return "/club/ER_index";

}

 
주소창에 /club/3 을 입력하면 NoSuchRequestHandlingMethodException 예외가 발생하는 경우가 있다.

참고로 web.xml에는  

<servlet-mapping>

    <servlet-name>dispatcher</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet-mapping>

    <servlet-name>dispatcher</servlet-name>

    <url-pattern>/club/*</url-pattern>

</servlet-mapping>

 와 같이 설정되어 있다.

해결책은 아래와 같다.

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

    <property name="alwaysUseFullPath" value="true" />

    ....

</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

    <property name="alwaysUseFullPath" value="true" />

    ....

</bean>

 
톰캣에서 Spring & Log4j 리스너 사용 시 발생가능한 문제

예외 메시지 1 : Error reading tld listeners java.lang.NullPointerException
예외 메시지 2 : ApplicationContext.java:647 : Shutting down log4j

문제는 org.springframework.web.util.Log4jConfigListener가 org.springframework.web.context.ContextLoaderListener 전에 로드되어야 한다는 점이다.

샘플 web.xml

SomeService.java 에서 ....

SomeDao.java 에서 ....

OtherDao.java 에서 .....

+ Recent posts