From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001
From: 田源 <tianyuan@vci-tech.com>
Date: 星期四, 03 四月 2025 14:35:02 +0800
Subject: [PATCH] 添加非密字段显示
---
Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/MappingApiJackson2HttpMessageConverter.java | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 108 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/MappingApiJackson2HttpMessageConverter.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/MappingApiJackson2HttpMessageConverter.java
new file mode 100644
index 0000000..d73f7ba
--- /dev/null
+++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/MappingApiJackson2HttpMessageConverter.java
@@ -0,0 +1,108 @@
+package org.springblade.core.tool.jackson;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springblade.core.tool.utils.Charsets;
+import org.springframework.http.HttpOutputMessage;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.HttpMessageNotWritableException;
+import org.springframework.lang.NonNull;
+import org.springframework.lang.Nullable;
+import org.springframework.util.StreamUtils;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 閽堝 api 鏈嶅姟瀵� android 鍜� ios 澶勭悊鐨� 鍒嗚鍐欑殑 jackson 澶勭悊
+ *
+ * <p>
+ * 1. app 绔笂鎶ユ暟鎹槸 浣跨敤 readObjectMapper
+ * 2. 杩斿洖缁� app 绔殑鏁版嵁浣跨敤 writeObjectMapper
+ * 3. 濡傛灉鏄繑鍥炲瓧绗︿覆锛岀洿鎺ョ浉搴旓紝涓嶅仛 json 澶勭悊
+ * </p>
+ *
+ * @author L.cm
+ */
+public class MappingApiJackson2HttpMessageConverter extends AbstractReadWriteJackson2HttpMessageConverter {
+
+ @Nullable
+ private String jsonPrefix;
+
+ public MappingApiJackson2HttpMessageConverter(ObjectMapper objectMapper, BladeJacksonProperties properties) {
+ super(objectMapper, initWriteObjectMapper(objectMapper, properties), initMediaType(properties));
+ }
+
+ private static List<MediaType> initMediaType(BladeJacksonProperties properties) {
+ List<MediaType> supportedMediaTypes = new ArrayList<>();
+ supportedMediaTypes.add(MediaType.APPLICATION_JSON);
+ supportedMediaTypes.add(new MediaType("application", "*+json"));
+ // 鏀寔 text 鏂囨湰锛岀敤浜庢姤鏂囩鍚�
+ if (Boolean.TRUE.equals(properties.getSupportTextPlain())) {
+ supportedMediaTypes.add(MediaType.TEXT_PLAIN);
+ }
+ return supportedMediaTypes;
+ }
+
+ private static ObjectMapper initWriteObjectMapper(ObjectMapper readObjectMapper, BladeJacksonProperties properties) {
+ // 鎷疯礉 readObjectMapper
+ ObjectMapper writeObjectMapper = readObjectMapper.copy();
+ // 澶ф暟瀛� 杞� 瀛楃涓�
+ if (Boolean.TRUE.equals(properties.getBigNumToString())) {
+ writeObjectMapper.registerModules(BladeNumberModule.INSTANCE);
+ }
+ // null 澶勭悊
+ if (Boolean.TRUE.equals(properties.getNullToEmpty())) {
+ writeObjectMapper.setSerializerFactory(writeObjectMapper.getSerializerFactory().withSerializerModifier(new BladeBeanSerializerModifier()));
+ writeObjectMapper.getSerializerProvider().setNullValueSerializer(BladeBeanSerializerModifier.NullJsonSerializers.STRING_JSON_SERIALIZER);
+ }
+ return writeObjectMapper;
+ }
+
+ @Override
+ protected void writeInternal(@NonNull Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
+ // 濡傛灉鏄瓧绗︿覆锛岀洿鎺ュ啓鍑�
+ if (object instanceof String) {
+ Charset defaultCharset = this.getDefaultCharset();
+ Charset charset = defaultCharset == null ? Charsets.UTF_8 : defaultCharset;
+ StreamUtils.copy((String) object, charset, outputMessage.getBody());
+ } else {
+ super.writeInternal(object, type, outputMessage);
+ }
+ }
+
+ /**
+ * Specify a custom prefix to use for this view's JSON output.
+ * Default is none.
+ *
+ * @param jsonPrefix jsonPrefix
+ * @see #setPrefixJson
+ */
+ public void setJsonPrefix(@Nullable String jsonPrefix) {
+ this.jsonPrefix = jsonPrefix;
+ }
+
+ /**
+ * Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default is false.
+ * <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
+ * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
+ * This prefix should be stripped before parsing the string as JSON.
+ *
+ * @param prefixJson prefixJson
+ * @see #setJsonPrefix
+ */
+ public void setPrefixJson(boolean prefixJson) {
+ this.jsonPrefix = (prefixJson ? ")]}', " : null);
+ }
+
+ @Override
+ protected void writePrefix(@NonNull JsonGenerator generator, @NonNull Object object) throws IOException {
+ if (this.jsonPrefix != null) {
+ generator.writeRaw(this.jsonPrefix);
+ }
+ }
+
+}
--
Gitblit v1.9.3