Jersey Unified Exception Handling

Source: Internet
Author: User

As we all know, the Java service provider to the service requester should be a specific format of the data, but not the exception stack similar information, then jersey, how to add uniform exception handling?

For Jersey booting if the implementation class implements the ResourceConfig interface, you can register the custom exception-handling class.

/*** Function Description: Unified exception Handling Filter *@return<br/> * Revision History:<br/> * 1. [September 09, 2016 11:11] Create method by HW*/@Provider Public classExceptionhandlerImplementsExceptionmapper<exception>{@Override PublicResponse toresponse (Exception e) {baseresponse Response=NewBaseresponse (); if(Einstanceofsuperexception) {superexception exp=(superexception) e;            Response.seterrcode (Exp.getcode ());        Response.setmsg (Exp.getmessage ()); } Else{Response.seterrcode (ErrorsDiscriptor.InvalidAuthCode.code ());        Response.setmsg (ErrorsDiscriptor.InternalService.message ()); }        returnResponse.ok (). Entity (Response). build (); }}

In the startup class, the class needs to be registered in rest, and the Exceptionhandler class is automatically loaded during the Jersey startup, if an exception is thrown when the caller requests the rest interface. The exception is captured by Exceptionhandler and the contract format error message is processed uniformly to the caller

@ApplicationPath ("/rest") Public classMyApplicationextendsResourceConfig {/*** MyApplication Construction method*/     PublicMyApplication () {//initializes the resource, initialized in the form of a specified package, separated by semicolons between multiple packagesPackages ("Com.timevale.esign.openapi.tech.resource"); //Packages ("Com.timevale.esign.openapi.tech.opensvc.resource");register (Exceptionhandler.class); //Registering Spring filterRegister (Requestcontextfilter.class); //register a data converter that supports automatic conversion between the JSON format and the bean for parameter and return informationRegister (Jacksonjsonprovider.class); //registering for requests that support Multipart-formdata formatRegister (org.glassfish.jersey.media.multipart.MultiPartFeature.class); //Initializing the databaseenvironmentinitializer.instance (). Initonce (); }}

The second way, if it is implemented in the Servlet configuration jersey-spring in Web. XML, specify jersey to scan the package that the custom Exceptionhandler is in, Exceptionhandler implementation is the same as above.

<servlet>        <servlet-name>jersey-spring</servlet-name>        <servlet-class> com.sun.jersey.spi.spring.container.servlet.springservlet</servlet-class>        <init-param >            <param-name>com.sun.jersey.config.property.packages</param-name>            <param-value> Cn.tsign.www.openapi.rest.resource; cn.tsign.www.openapi.rest.filter</param-value>        </init-param>        <init-param>            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>            <param-value> true</param-value>        </init-param>        <load-on-startup>2</load-on-startup >    </servlet>

Jersey Unified Exception Handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.