Announcement Announcement Module
No announcement yet.
ContentNegotiatingViewResolver + ExceptionResolver Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

    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. (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
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="order" value="1" />
        <property name="exceptionMappings">
            <prop key="BusinessException">forward:/app/error/4XX</prop>
            <prop key="SystemException">forward:/app/error/5XX</prop>
        <property name="defaultErrorView" value="/error/5XX"/>
      <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" >
        <property name="order" value="0" />
        <property name="defaultContentType" value="application/json" />
        <property name="mediaTypes">
            <entry key="xml" value="application/xml" /> 
            <entry key="json" value="application/json" />
        <property name="defaultViews">
            <ref bean="xmlMarshallView" /> 
            <ref bean="mappingJacksonJsonView" />
    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.