Skip to content

Commit

Permalink
✨ 支持 JsonView 和 R 统一封装返回 github #53
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Aug 24, 2023
1 parent 329e278 commit 5df1a40
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,4 +36,10 @@ public SpringContextUtil springContextUtil() {
return new SpringContextUtil();
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizerEnableDefaultViewInclusion() {
// 支持 R 返回 json 渲染,开启默认的 Jackson 会将所有属性包含在序列化或反序列化的结果中,无论是否定义了视图。
return builder -> builder.defaultViewInclusion(true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<Object> {

@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return returnType.hasMethodAnnotation(JsonView.class);
}

@Override
public Object beforeBodyWrite(Object body,
MethodParameter returnType,
MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {
// 如果返回的是 R
if (body instanceof R<?>) {
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(body);
JsonView jsonView = returnType.getMethodAnnotation(JsonView.class);
mappingJacksonValue.setSerializationView(jsonView.value()[0]);
return mappingJacksonValue;
}
return body;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/

@NonNullApi
@NonNullFields
package net.dreamlu.mica.lite.jackson;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.nats.client.*;
import io.nats.client.api.ConsumerConfiguration;
import io.nats.client.api.DeliverPolicy;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.utils.Exceptions;
import net.dreamlu.mica.core.utils.StringUtil;
Expand All @@ -36,7 +35,6 @@
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

/**
* nats JetStream 监听器处理
Expand Down

0 comments on commit 5df1a40

Please sign in to comment.