Announcement Announcement Module
Collapse
No announcement yet.
Automate JackSON conversion in ExceptionHandler? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Automate JackSON conversion in ExceptionHandler?

    Hi,

    I am writing a RESTful web service. All the outputs of the web service is currently in JSON format and I make use of MappingJacksonHttpMessageConverter to convert all my positive output to JSON.

    I facing some problems on the error handling part. As I read from internet, @ExceptionHandler only can returns String / MAV. I could not apply the @ResponseBody that I used in other method to auto-convert my output to JSON. My snapshot of code as below (modified from Spring Doc v3.0.0, Section 18.2.6):

    Code:
    @Controller
    public class SimpleController {
    
     // other controller method omitted
    
     @ExceptionHandler(IOException.class)
     public String handleIOException(IOException ex, HttpServletRequest request) {
        // Convert 'ex.getMessage()' with to ErrorMessage with Object Mapper
        ErrorMessage errorMessage = new ErrorMessage(ex.getMessage());
        String ret = mapper.writeValueAsString(errorMessage);
        ....
        return ret;
     }
    Code:
    @XmlRootElement(name="errorMessage")
    public class ErrorMessage {
    	
    	private String error_message;
    
    	
    	public ErrorMessage() {}
    	
    	public ErrorMessage(String error_message) {
    		this.error_message = error_message;
    	}
    
    	public String getError_message() {
    		return error_message;
    	}
    
    	public void setError_message(String error_message) {
    		this.error_message = error_message;
    	}
    
    }
    The expecting output is (in JSON)
    {"error_message":"this is the real exception error message."}

    My questions is - is that any idea to automate the process besides doing to conversion in every @ExceptionHandler method?

    Thanks for your advises.

  • #2
    Hi,

    I found a half-way solution for my case. I added AnnotationMethodHandlerExceptionResolver in the servlet.xml

    Code:
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
        <property name="messageConverters">
            <list>
    	    <ref bean="marshallingConverter" />
    	    <ref bean="jsonConverter" />
             </list>
        </property>
    </bean>

    And my Controller code like below:

    Code:
    @Controller
    public class SimpleController {
    
        // other controller method omitted
    
        @ExceptionHandler(IOException.class)
        public @ResponseBody IOException handleIOException(IOException ex, HttpServletRequest request) {
            return ex;
        }
    }

    By doing so, I can get the exception with message in JSON

    Code:
    {"cause":null,"message":"my custom exception message","localizedMessage":"my custom exception message","stackTrace":[{"className":"sun.reflect.NativeMethodAccessorImpl","fileName":null,"lineNumber":-2,"methodName":"invoke0","nativeMethod":true},{"className":"sun.reflect.NativeMethodAccessorImpl","fileName":null,"lineNumber":-1,"methodName":"invoke","nativeMethod":false}, 
    .....
    < Long Long Stack Trace>
    .....
    ]
    Any idea to take out the 'stack trace' from the JSON ? Actually, I only interest to send BOLD text as JSON to the REST client.
    I would like to log the stack trace in REST server log, but not sending the stack trace to the rest client (Its meaningless to REST Client)

    Thanks for viewing and/or advises.

    Comment

    Working...
    X