|
@@ -8,10 +8,13 @@ import com.alibaba.fastjson.serializer.PropertyFilter;
|
|
import com.alibaba.fastjson.serializer.SerializeFilter;
|
|
import com.alibaba.fastjson.serializer.SerializeFilter;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
|
|
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
|
|
|
|
+import org.hswebframework.utils.ClassUtils;
|
|
import org.hswebframework.web.ThreadLocalUtils;
|
|
import org.hswebframework.web.ThreadLocalUtils;
|
|
import org.hswebframework.web.controller.message.ResponseMessage;
|
|
import org.hswebframework.web.controller.message.ResponseMessage;
|
|
import org.hswebframework.utils.StringUtils;
|
|
import org.hswebframework.utils.StringUtils;
|
|
import org.hswebframework.web.convert.CustomMessageConverter;
|
|
import org.hswebframework.web.convert.CustomMessageConverter;
|
|
|
|
+import org.hswebframework.web.dict.DictSupportApi;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.Ordered;
|
|
import org.springframework.core.Ordered;
|
|
import org.springframework.http.HttpInputMessage;
|
|
import org.springframework.http.HttpInputMessage;
|
|
import org.springframework.http.HttpOutputMessage;
|
|
import org.springframework.http.HttpOutputMessage;
|
|
@@ -32,6 +35,9 @@ import java.util.Set;
|
|
|
|
|
|
public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> implements Ordered {
|
|
public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> implements Ordered {
|
|
|
|
|
|
|
|
+ @Autowired(required = false)
|
|
|
|
+ private DictSupportApi dictSupportApi;
|
|
|
|
+
|
|
public final static Charset UTF8 = Charset.forName("UTF-8");
|
|
public final static Charset UTF8 = Charset.forName("UTF-8");
|
|
|
|
|
|
private Charset charset = UTF8;
|
|
private Charset charset = UTF8;
|
|
@@ -80,8 +86,8 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
|
|
}
|
|
}
|
|
|
|
|
|
public Object readByBytes(Class<?> clazz, byte[] bytes) {
|
|
public Object readByBytes(Class<?> clazz, byte[] bytes) {
|
|
- if(clazz==String.class){
|
|
|
|
- return new String(bytes,charset);
|
|
|
|
|
|
+ if (clazz == String.class) {
|
|
|
|
+ return new String(bytes, charset);
|
|
}
|
|
}
|
|
if (null != converters) {
|
|
if (null != converters) {
|
|
CustomMessageConverter converter = converters.stream()
|
|
CustomMessageConverter converter = converters.stream()
|
|
@@ -92,12 +98,15 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
|
|
return converter.convert(clazz, bytes);
|
|
return converter.convert(clazz, bytes);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
|
|
|
|
|
|
+ Object object = JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
|
|
|
|
+ if (dictSupportApi != null) {
|
|
|
|
+ object = dictSupportApi.unwrap(object);
|
|
|
|
+ }
|
|
|
|
+ return object;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException,
|
|
|
|
- HttpMessageNotReadableException {
|
|
|
|
|
|
+ protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
InputStream in = inputMessage.getBody();
|
|
InputStream in = inputMessage.getBody();
|
|
byte[] buf = new byte[1024];
|
|
byte[] buf = new byte[1024];
|
|
@@ -122,8 +131,14 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
|
|
String callback = ThreadLocalUtils.getAndRemove("jsonp-callback");
|
|
String callback = ThreadLocalUtils.getAndRemove("jsonp-callback");
|
|
if (obj instanceof ResponseMessage) {
|
|
if (obj instanceof ResponseMessage) {
|
|
ResponseMessage message = (ResponseMessage) obj;
|
|
ResponseMessage message = (ResponseMessage) obj;
|
|
|
|
+ if (dictSupportApi != null) {
|
|
|
|
+ message.setResult(dictSupportApi.wrap(message.getResult()));
|
|
|
|
+ }
|
|
text = JSON.toJSONString(obj, parseFilter(message), features);
|
|
text = JSON.toJSONString(obj, parseFilter(message), features);
|
|
} else {
|
|
} else {
|
|
|
|
+ if (dictSupportApi != null) {
|
|
|
|
+ obj = dictSupportApi.wrap(obj);
|
|
|
|
+ }
|
|
text = JSON.toJSONString(obj, features);
|
|
text = JSON.toJSONString(obj, features);
|
|
}
|
|
}
|
|
if (!StringUtils.isNullOrEmpty(callback)) {
|
|
if (!StringUtils.isNullOrEmpty(callback)) {
|
|
@@ -136,15 +151,14 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
|
|
|
|
- HttpMessageNotWritableException {
|
|
|
|
|
|
+ protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
|
|
OutputStream out = outputMessage.getBody();
|
|
OutputStream out = outputMessage.getBody();
|
|
byte[] bytes = converter(obj).getBytes(charset);
|
|
byte[] bytes = converter(obj).getBytes(charset);
|
|
out.write(bytes);
|
|
out.write(bytes);
|
|
out.flush();
|
|
out.flush();
|
|
}
|
|
}
|
|
|
|
|
|
- protected static SerializeFilter[] parseFilter(ResponseMessage<?> responseMessage) {
|
|
|
|
|
|
+ public static SerializeFilter[] parseFilter(ResponseMessage<?> responseMessage) {
|
|
List<SerializeFilter> filters = new ArrayList<>();
|
|
List<SerializeFilter> filters = new ArrayList<>();
|
|
if (responseMessage.getIncludes() != null) {
|
|
if (responseMessage.getIncludes() != null) {
|
|
for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getIncludes().entrySet()) {
|
|
for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getIncludes().entrySet()) {
|