1. Generate an exception code
String a = "a3"; intb = Integer.parseInt(a);
2. Custom exception class
Inheritance relationship: DataAccessException inherits ServiceException extends RestWebServiceException inherits Exception
The advantage of doing this is: dao layer exception throws DataAccessException so that the service layer can cover (subclass exception) and then throw it online, to the rest layer, and throw RestWebServiceException (you can also get the service layer exception)
Finally, it is unified by the interceptor, which intercepts all exceptions and then handles them uniformly. The benefit of this is that you can print the wrong number of lines - good for locating the problem.
If you try catch yourself, then the error is reported in try and only the number of lines is recorded when throw Exception, that is, it can record the line of throw and cannot locate which line in the try.
publicclass DataAccessException extends ServiceException { privatestaticfinallongserialVersionUID = 1L; private String retCd; // Return code corresponding to exception private String msgDes; // Description information corresponding to the exception public DataAccessException() { super(); } public DataAccessException(String message) { super(message); msgDes = message; } public DataAccessException(String retCd, String message) { super(); this.retCd = retCd; this.msgDes = message; } public DataAccessException( Throwable throwable) { super(throwable); } public DataAccessException(String message,Throwable throwable) { super(message,throwable); msgDes = message; } public DataAccessException(String retCd, String message,Throwable throwable) { super(message,throwable); this.retCd = retCd; this.msgDes = message; }
publicclass ServiceException extends RestWebServiceException { privatestaticfinallongserialVersionUID = 1L; private String retCd; // Return code corresponding to exception private String msgDes; // Description information corresponding to the exception . . . Ditto
publicclass RestWebServiceException extends Exception { privatestaticfinallongserialVersionUID = 1L; private String retCd; // Return code corresponding to exception private String msgDes; // Description information corresponding to the exception
Let's see how the rest layer handles the layer and service layer
//The service layer can throw an exception directly publicint addComUser(ComUser user) throwsServiceException{ return comUserMapper.insert(user); }
See how the interceptor is implemented: /** * Exception interceptor * @author:andy * @version:1.0 */ publicclass ErrorInterceptor extends AbstractPhaseInterceptor<Message> { Logger logger = LoggerFactory.getLogger(getClass()); public ErrorInterceptor(String phase) { super(phase); } public ErrorInterceptor(){ super(Phase.RECEIVE); } @Override publicvoid handleMessage(Message msg) throws Fault { } //Error output error message and stack information publicvoid handleFault(Message message) { //Can intercept all exceptions except WebApplicationException Exception exeption=message.getContent(Exception.class); logger.error(exeption.getMessage(), exeption);//Only logger.xxx("",e); 2 parameters can print exception details, and functions with other parameters are not good } }
//The rest layer also throws exceptions directly, don't try catch yourself. @GET @Path("/getUserById/{id}") public ComUser getComUserById(@PathParam("id") intid) throws RestWebServiceException{ ComUser user = null; user = comUserService.getComUserById(id); // business return if(null == user){ ResponseBuilder builder = Response.status(Status.BAD_REQUEST); builder.type(MediaType.APPLICATION_JSON); builder.entity(new ResultJson(ResultJson.FAILURE, "Access data does not exist")); thrownew WebApplicationException(builder.build()); } return user; }