|
@@ -0,0 +1,63 @@
|
|
|
+package com.yonge.cooleshow.config;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.microsvc.toolkit.common.response.template.ExtResponse;
|
|
|
+import com.microsvc.toolkit.common.response.template.JSONResponse;
|
|
|
+import com.microsvc.toolkit.common.response.template.Response;
|
|
|
+import com.microsvc.toolkit.common.response.template.SysResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.http.HttpOutputMessage;
|
|
|
+import org.springframework.http.converter.HttpMessageNotWritableException;
|
|
|
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Eric.Shang on 3/3/17.
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class MappingJSONHttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
|
|
+
|
|
|
+ public MappingJSONHttpMessageConverter() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public MappingJSONHttpMessageConverter(ObjectMapper objectMapper) {
|
|
|
+ super(objectMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义JSON异常封装
|
|
|
+ * @param object Object
|
|
|
+ * @param type Type
|
|
|
+ * @param outputMessage HttpOutputMessage
|
|
|
+ * @throws IOException IOException
|
|
|
+ * @throws HttpMessageNotWritableException HttpMessageNotWritableException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
|
|
|
+ throws IOException, HttpMessageNotWritableException {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (object instanceof ExtResponse) {
|
|
|
+ // 拓展返回数据结构
|
|
|
+ object = ((ExtResponse<?>) object).data();
|
|
|
+
|
|
|
+ } else if (object instanceof Response) {
|
|
|
+ // 自定义标准数据结构
|
|
|
+ object = SysResponse.from((Response<?>) object);
|
|
|
+
|
|
|
+ } else if (object instanceof JSONResponse) {
|
|
|
+ // 三方标准数据结构,
|
|
|
+ object = JSON.toJSON(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ object = SysResponse.sysResponse();
|
|
|
+ log.error("jsonObject={}", object, e);
|
|
|
+ }
|
|
|
+ super.writeInternal(object, type, outputMessage);
|
|
|
+ }
|
|
|
+}
|