Announcement Announcement Module
Collapse
No announcement yet.
ContentNegotiatingViewResolver + ExceptionResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ContentNegotiatingViewResolver + ExceptionResolver

    I'am newly studying REST && Spring MVC 3.

    I have one question : How to handle exception when using ContentNegotiatingViewResolver ?

    I found this is asked about two year ago, but still no satisfied solution:
    http://forum.springsource.org/showthread.php?t=77417

    The one method I can use now is :
    ContentNegotiatingViewResolver + @ExceptionHandler

    but @ExceptionHandler is not so convenience because:
    1. Not accept ResponseEntity<T> as return value.
    http://forum.springsource.org/showthread.php?t=100755
    https://jira.springsource.org/browse/SPR-6008 (I'm using spring 3.0.5)
    But, we still can use Map as return value and response.setStatus(int) instead.

    2. I have to write multiple @ExceptionHandler method for each @Controller class, unless using super class for this.



    ------------------------------------------------------------------------------
    I still tried this :
    ContentNegotiatingViewResolver + SimpleMappingExceptionResolver
    Code:
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="order" value="1" />
        <property name="exceptionMappings">
          <props>
            <prop key="BusinessException">forward:/app/error/4XX</prop>
            <prop key="SystemException">forward:/app/error/5XX</prop>
          </props>
        </property>
        <property name="defaultErrorView" value="/error/5XX"/>
      </bean>
      <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" >
        <property name="order" value="0" />
        <property name="defaultContentType" value="application/json" />
        <property name="mediaTypes">
          <map>
            <entry key="xml" value="application/xml" /> 
            <entry key="json" value="application/json" />
          </map>
        </property>
        <property name="defaultViews">
          <list>
            <ref bean="xmlMarshallView" /> 
            <ref bean="mappingJacksonJsonView" />
          </list>
        </property>
      </bean>
    But, I failed. By 《Spring Framework Reference Documentation 3.0》 15.5 Resolving views,
    only UrlBasedViewResolver and its sub class can resolve "redirect:" and "forward:" prefix.
    Actually, it seems ContentNegotiatingViewResolver does not concern the view names.

    So , What is the best practice about Exception Handling with REST and Content Negotiation ? (Ask again, )
    Is ContentNegotiatingViewResolver can resolve "forward:" prefix in future?


    -----------------------------
    I think I misused the sign on title, it should be "Question", not "Thumbs down". (It also like a red question sign, )
    Last edited by btpka3; May 8th, 2011, 09:09 PM.
Working...
X