All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.jersey.guice.providers.exception.GsonDefaultExceptionMapper Maven / Gradle / Ivy

The newest version!
package com.netflix.jersey.guice.providers.exception;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response;

import com.google.gson.Gson;

/**
 * Default mapper returns error 500 and a JSON response containing,
 * {
 *  code : 500,
 *  class : ...
 *  stack : ...
 * }
 * 
 * An alternative default mapper can be used by binding the implementation
 * to DefaultThrowableExceptionMapper.class in a guice module.
 * 
 * @author elandau
 *
 */
public class GsonDefaultExceptionMapper implements DefaultThrowableExceptionMapper {

    @Override
    public Response toResponse(Throwable exception) {
        Map info = new HashMap();
        if (exception.getCause() != null)
            info.put("class", exception.getCause().getClass().getCanonicalName());
        else 
            info.put("class", exception.getClass().getCanonicalName());
        info.put("code",  500);
        info.put("stack", exception.getStackTrace());
        
        return Response.status(500).entity(new Gson().toJson(info)).build();
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy