0

This is my SqlExceptionMapper

@Provider
public class SqlExceptionMapper implements ExceptionMapper<SQLException> {

    @Override
    public Response toResponse(SQLException e){
        return ResponseBuilder.error(Response.Status.UNAUTHORIZED.getStatusCode(), "success", null, e.getMessage());        
    }

}

Here I am throwing exception in catch block of my function

public void myfunction()throws SQLException {
    try {       
        // some code that throws an sql exception 
    } catch (SQLException e) {
        final Logger logger = Logger.getLogger(Image.class);
        logger.error("This is error", e);
        throw new SQLException();
    }
}

This is my web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application -image Rest API's</display-name>
  <servlet>  
        <servlet-name>jersey-serlvet</servlet-name>  
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
        <init-param>       
            <param-name>com.sun.jersey.config.property.packages</param-name>       
            <param-value>com.image.controller;com.image.exceptionmapper</param-value>  
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>     
        </init-param>  
        <load-on-startup>1</load-on-startup> 
    </servlet>
    <servlet-mapping>  
        <servlet-name>jersey-serlvet</servlet-name>  
        <url-pattern>/*</url-pattern> 
    </servlet-mapping>
</web-app>

I have debugged my code and my catch block is throwing SQLException, the only problem is it is not mapping to the mapper which I have created.

angryip
  • 2,140
  • 5
  • 33
  • 67
Shivkumar Mallesappa
  • 2,875
  • 7
  • 41
  • 68
  • I don't think you can register the exceptionmapper as "packages". I don't know which version of jersey you're using, but it is usually more handy to use a ResourceConfig object, see [this question](http://stackoverflow.com/questions/31501110/registering-a-provider-programmatically-in-jersey-which-implements-exceptionmapp). Hope it helps. – lrnzcig Apr 11 '16 at 15:42
  • Which jersey version do you use, 1.x? What is the package name of the class `SqlExceptionMapper`? What happens - did you get a 500? – Meiko Rachimow Apr 11 '16 at 21:38
  • I recompiled my code with different name I changed it from sqlExceptionMapper with SQLExceptionMapper and it is working. Thanks – Shivkumar Mallesappa Apr 12 '16 at 03:40
  • @ShivkumarMallesappa you need to either set this question as resolved, and provide your solution. – angryip Jul 21 '16 at 14:51

1 Answers1

0

I think I did some thing wrong with the naming.

I renamed my class from sqlExceptionMapper to SQLExceptionMapper and recompiled it.It is working for me.

Thanks

Shivkumar Mallesappa
  • 2,875
  • 7
  • 41
  • 68