java前后端分离使用注解优雅的返回实体部分属性-成都快上网建站

java前后端分离使用注解优雅的返回实体部分属性

#第一个注解

为召陵等地区用户提供了全套网页设计制作服务,及召陵网站建设行业解决方案。主营业务为网站设计、成都做网站、召陵网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* @Description:

* @Author: ckk

* @CreateDate: 2019/9/17 17:15

*/

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface JSONS {

JSONField[] value();

}

#第二个注解

import java.lang.annotation.*;

/**

* @Description:

* @Author: ckk

* @CreateDate: 2019/9/17 17:16

*/

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Repeatable(JSONS.class)

public @interface JSONField {

Class type();

String[] include() default {""};

String[] filter() default {""};

}

#解析器

import com.fasterxml.jackson.annotation.JsonFilter;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.ser.BeanPropertyFilter;

import com.fasterxml.jackson.databind.ser.FilterProvider;

import com.fasterxml.jackson.databind.ser.PropertyFilter;

import com.fasterxml.jackson.databind.ser.PropertyWriter;

import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;

import java.util.*;

/**

* @Description:

* @Author: ckk

* @CreateDate: 2019/9/17 17:19

*/

@JsonFilter("JacksonFilter")

public class JacksonJsonFilter extends FilterProvider {

Map, Set> includeMap = new HashMap();

Map, Set> filterMap = new HashMap();

public JacksonJsonFilter() {

}

public void include(Class type, String[] fields) {

this.addToMap(this.includeMap, type, fields);

}

public void filter(Class type, String[] fields) {

this.addToMap(this.filterMap, type, fields);

}

private void addToMap(Map, Set> map, Class type, String[] fields) {

Set fieldSet = (Set) map.getOrDefault(type, new HashSet());

fieldSet.addAll(Arrays.asList(fields));

map.put(type, fieldSet);

}

public BeanPropertyFilter findFilter(Object filterId) {

throw new UnsupportedOperationException("Access to deprecated filters not supported");

}

public PropertyFilter findPropertyFilter(Object filterId, Object valueToFilter) {

return new SimpleBeanPropertyFilter() {

public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer) throws Exception {

if (JacksonJsonFilter.this.apply(pojo.getClass(), writer.getName())) {

writer.serializeAsField(pojo, jgen, prov);

} else if (!jgen.canOmitFields()) {

writer.serializeAsOmittedField(pojo, jgen, prov);

}

}

};

}

public boolean apply(Class type, String name) {

Set includeFields = (Set) this.includeMap.get(type);

Set filterFields = (Set) this.filterMap.get(type);

if (includeFields != null && includeFields.contains(name)) {

return true;

} else if (filterFields != null && !filterFields.contains(name)) {

return true;

} else {

return includeFields == null && filterFields == null;

}

}

}

序列化器

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;

/**

* @Description:

* @Author: ckk

* @CreateDate: 2019/9/17 17:18

*/无锡做人流手术多少钱 http://www.ytsg029.com/

public class CustomerJsonSerializer {

ObjectMapper mapper = new ObjectMapper();

JacksonJsonFilter jacksonFilter = new JacksonJsonFilter();

public CustomerJsonSerializer() {

}

public void filter(Class clazz, String[] include, String[] filter) {

if (clazz != null) {

if (include.length > 1 || include.length == 1 && org.apache.commons.lang3.StringUtils.isNotBlank(include[0])) {

this.jacksonFilter.include(clazz, include);

}

if (filter.length > 1 || filter.length == 1 && StringUtils.isNotBlank(filter[0])) {

this.jacksonFilter.filter(clazz, filter);

}

this.mapper.addMixIn(clazz, this.jacksonFilter.getClass());

}

}

public String toJson(Object object) throws JsonProcessingException {

this.mapper.setFilterProvider(this.jacksonFilter);

return this.mapper.writeValueAsString(object);

}

public void filter(JSONField json) {

this.filter(json.type(), json.include(), json.filter());

}

}

#使用方法

@ApiOperation(value = “查询详情”, response = ZcPolicyVo.class)

@GetMapping(“getById/{id}”)

@JSONS({@JSONField(type = ZcPolicyVo.class, filter = {“base_id”, “pic”, “mobilecontent”,

“synchtime”, “url”, “policy_originid”, “base_updatetime”, “tags”, “audittime”, “creator”}),

@JSONField(type = SysAreaVo.class, include = {“base_id”, “base_name”}),

@JSONField(type = SysCodeVo.class, include = {“base_id”, “base_name”}),

@JSONField(type = ZcRelationVo.class, include = {“keyId”, “keyIdName”}),

@JSONField(type = ImUsersVo.class, include = {“userid”, “username”}),

@JSONField(type = ZcCentraldeptcodeVo.class, include = {“codeId”, “codeIdName”}),

@JSONField(type = ZcLocaldeptcodeVo.class, include = {“codeId”, “codeIdName”}),

@JSONField(type = PolicyKeywordsVo.class, include = {“keyId”, “keyIdName”}),

@JSONField(type = PolicyMapKeywordsVo.class, include = {“keyId”, “keyIdName”}),

@JSONField(type = ZcKeywordVo.class, include = {“baseId”, “title”})

})

public Message getById(@PathVariable(“id”) String id) {

try {

ZcPolicy zcPolicy = zcPolicyJpaService.getObjectByPK(id);

return Message.success(BeanutilsCopy.convertBean(zcPolicy, ZcPolicyVo.class));

} catch (Exception e) {

logger.error("查询详情异常:===》 " + e);

return Message.exception(e.getMessage());

}

}

#filter:排除某些属性

#include :包括某些属性


本文名称:java前后端分离使用注解优雅的返回实体部分属性
网址分享:http://kswjz.com/article/gipshp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流