.
[baibao-modules.git] / base / app-framework-webbox / src / main / java / com / baibao / webbox / advice / WebExceptionAdvice.java
blob077b276dacea0382bc9acc8a4ffc4924d009f2ad
1 package com.baibao.webbox.advice;
3 import javax.xml.ws.soap.SOAPFaultException;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.cxf.binding.soap.SoapFault;
8 import org.apache.cxf.interceptor.Fault;
9 import org.springframework.http.HttpHeaders;
10 import org.springframework.http.HttpStatus;
11 import org.springframework.http.ResponseEntity;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.WebDataBinder;
14 import org.springframework.web.bind.annotation.ControllerAdvice;
15 import org.springframework.web.bind.annotation.ExceptionHandler;
16 import org.springframework.web.bind.annotation.InitBinder;
17 import org.springframework.web.bind.annotation.ResponseBody;
18 import org.springframework.web.bind.annotation.ResponseStatus;
20 import com.baibao.data.*;
21 import com.baibao.exception.BusinessException;
22 import com.baibao.utils.JsonHelper;
23 import com.baibao.utils.Utils;
25 @ControllerAdvice(annotations = Controller.class)
26 public class WebExceptionAdvice
28 private static Log log = LogFactory.getLog(WebExceptionAdvice.class);
30 private Boolean turnErrorStatus = true;
32 @InitBinder
33 public void initBinder(WebDataBinder binder) {
34 binder.registerCustomEditor(java.util.Date.class, new DateEditor());
35 binder.registerCustomEditor(java.sql.Date.class, new DateEditor());
36 binder.registerCustomEditor(java.sql.Timestamp.class, new DateEditor());
39 public WebExceptionAdvice(String turnErrorStatus)
41 this(Boolean.valueOf(turnErrorStatus));
44 public WebExceptionAdvice(Boolean turnErrorStatus)
46 this.turnErrorStatus = turnErrorStatus;
48 log.info(String.format(
49 "WebExceptionAdvice.turnErrorStatus %s",
50 String.valueOf(this.turnErrorStatus)));
53 @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
54 @ExceptionHandler(BusinessException.class)
55 public ResponseEntity<String>
56 handleWebException(BusinessException exception)
58 log.error("BusinessException@WebExceptionAdvice", exception);
59 log.error(exception.getServerMsg());
62 HttpHeaders headers = new HttpHeaders();
64 headers.add("Content-Type", "application/json;charset=utf-8");
66 return new ResponseEntity<String>(
67 JsonHelper.formatJson(
68 exception.toResponse()), headers,
69 turnErrorStatus?
70 HttpStatus.INTERNAL_SERVER_ERROR: HttpStatus.OK);
73 @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
74 @ExceptionHandler(Exception.class)
75 public ResponseEntity<String>
76 handleWebException(Exception exception)
78 log.error("Exception@WebExceptionAdvice", exception);
80 Response<Object> response = new Response<Object>();
82 response.setRetCode(500);
83 response.setRetMsg(exception.toString());
85 HttpHeaders headers = new HttpHeaders();
87 headers.add("Content-Type", "application/json;charset=utf-8");
89 return new ResponseEntity<String>(
90 JsonHelper.formatJson(response), headers,
91 turnErrorStatus?
92 HttpStatus.INTERNAL_SERVER_ERROR: HttpStatus.OK);
95 @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
96 @ExceptionHandler(Fault.class)
97 public ResponseEntity<String> handleFault(SoapFault exception)
99 log.error("Exception@WebExceptionAdvice", exception);
101 Response<Object> response = new Response<Object>();
103 response.setRetCode(500);
107 response.setRetCode(Integer.parseInt(exception.getRole()));
108 } catch(Exception e)
111 response.setRetMsg(exception.getMessage());
113 HttpHeaders headers = new HttpHeaders();
115 headers.add("Content-Type", "application/json;charset=utf-8");
117 return new ResponseEntity<String>(
118 JsonHelper.formatJson(response), headers,
119 turnErrorStatus?
120 HttpStatus.INTERNAL_SERVER_ERROR: HttpStatus.OK);
123 @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
124 @ExceptionHandler(SOAPFaultException.class)
125 public ResponseEntity<String>
126 handleSOAPFaultException(SOAPFaultException exception)
128 log.error("Exception@WebExceptionAdvice", exception);
130 SoapFault fault =(SoapFault) exception.getCause();
132 return handleFault(fault);