diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/config/MicaLiteConfiguration.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/config/MicaLiteConfiguration.java index 0c8683f9e..4cf274d86 100644 --- a/mica-lite/src/main/java/net/dreamlu/mica/lite/config/MicaLiteConfiguration.java +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/config/MicaLiteConfiguration.java @@ -19,6 +19,7 @@ import net.dreamlu.mica.core.spring.SpringContextUtil; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; /** @@ -35,4 +36,10 @@ public SpringContextUtil springContextUtil() { return new SpringContextUtil(); } + @Bean + public Jackson2ObjectMapperBuilderCustomizer customizerEnableDefaultViewInclusion() { + // 支持 R 返回 json 渲染,开启默认的 Jackson 会将所有属性包含在序列化或反序列化的结果中,无论是否定义了视图。 + return builder -> builder.defaultViewInclusion(true); + } + } diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/jackson/JsonViewResultAdvice.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/jackson/JsonViewResultAdvice.java new file mode 100644 index 000000000..6dd7b91f4 --- /dev/null +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/jackson/JsonViewResultAdvice.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.lite.jackson; + +import com.fasterxml.jackson.annotation.JsonView; +import net.dreamlu.mica.core.result.R; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJacksonValue; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +/** + * 使 jackson view 在使用 R 了之后生效 + * + * @author L.cm + */ +@RestControllerAdvice +public class JsonViewResultAdvice implements ResponseBodyAdvice