In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve jackson based on serialization". In daily operation, I believe many people have doubts about how to achieve jackson based on serialization. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve jackson based on serialization". Next, please follow the editor to study!
Implementation based on serialization
Fastjson implementation
The idea of realization is:
Custom annotations that allow users to customize desensitization methods for attributes of entity classes
Intercept attribute annotations based on ValueFilter, and replace desensitization with multiple value
To serialize an object using json is to specify a custom serialization Filter
The core code is as follows
Custom Annotation Desensitization
@ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) public @ interface Desensitization {/ * * desensitization rules * * @ return * / DesensitionEnum desensitionEnum ();}
Desensitization method
/ * * desensitization function interface * / public interface Desensitizer extends Function {} @ Getterpublic enum DesensitionEnum {/ * * user name desensitization * / USERNAME ("userName", "user name", s-> s.replaceAll ("(\ S)\ S (\\ S2")), / * * ID card number desensitization * / ID_CARD ("idCard") "15 or 18 ID card number", s-> s.replaceAll ("(\ d {4})\\ d {10} (\\ w {4})", "$1 mobile phone number desensitization * / PHONE (" phone "," cell phone number ", s-> s.replaceAll (" (\ d {3})\\ d {4} (\\ d {4}) " )), / * * address desensitization * / ADDRESS ("address", "address desensitization", s-> s.replaceAll ("(\\ S {8})\\ S {4} (\\ S*)\\ S {4}", "$1 # address desensitization *") String fieldName; String fieldDescribe; private final Desensitizer desensitizer; DesensitionEnum (String fieldName, String fieldDescribe, Desensitizer desensitizer) {this.fieldName = fieldName; this.fieldDescribe = fieldDescribe; this.desensitizer = desensitizer;}}
Specific interception
Public class FastjsonDesensitizeFilter implements ValueFilter {@ Override public Object process (Object object, String name, Object value) {if (null = = value | |! (value instanceof String) | | ((String) value). Length () = = 0) {return value;} try {Field field = object.getClass () .getDeclaredField (name); Desensitization desensitization If (String.class! = field.getType () | (desensitization = field.getAnnotation (Desensitization.class)) = = null) {return value;} return desensitization.desensitionEnum () .getDesensitizer () .apply ((String) value);} catch (Exception e) {return value;}
Jackson implementation
Explanation:
Get the comments on the attributes of the object, and get the corresponding desensitization rule types according to the attributes.
Value replacement by rule type
Custom annotation
@ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) @ JsonSerialize (using = JacksonDesensitize.class) @ JacksonAnnotationsInsidepublic @ interface Desensitization {/ * * desensitization rules * * @ return * / DesensitionEnum desensitionEnum ();} package com.demo.desensitization;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.BeanProperty;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.JsonSerializer Import com.fasterxml.jackson.databind.SerializerProvider;import com.fasterxml.jackson.databind.ser.ContextualSerializer;import java.io.IOException;import java.util.Objects;public class JacksonDesensitize extends JsonSerializer implements ContextualSerializer {private DesensitionEnum desensitionEnum; @ Override public void serialize (String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {jsonGenerator.writeString (desensitionEnum.getDesensitizer (). Apply ((String) value)) } @ Override public JsonSerializer createContextual (SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {if (beanProperty! = null) {/ / non-String directly skip if (Objects.equals (beanProperty.getType (). GetRawClass (), String.class)) {/ / get comment information Desensitization annotation = beanProperty.getAnnotation (Desensitization.class) If (annotation = = null) {annotation = beanProperty.getContextAnnotation (Desensitization.class);} if (annotation! = null) {/ / get the value on the annotation and assign this.desensitionEnum = annotation.desensitionEnum (); return this }} return serializerProvider.findValueSerializer (beanProperty.getType (), beanProperty);} return serializerProvider.findNullValueSerializer (null);}}
Execution result
Package com.demo.desensitization;import lombok.Data;import java.io.Serializable;@Datapublic class UserDTO implements Serializable {@ Desensitization (desensitionEnum=DesensitionEnum.ID_CARD) private String idCard; @ Desensitization (desensitionEnum=DesensitionEnum.USERNAME) private String username; @ Desensitization (desensitionEnum=DesensitionEnum.PHONE) private String phone; @ Desensitization (desensitionEnum=DesensitionEnum.ADDRESS) private String address;} public static void testFast () {FastJsonConfig fastJsonConfig = new FastJsonConfig (); fastJsonConfig.setSerializerFeatures (SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect) FastJsonConfig.setSerializeFilters (new FastjsonDesensitizeFilter ()); UserDTO userDTO=new UserDTO (); userDTO.setUsername ("Zhang San"); userDTO.setPhone ("15669057552"); userDTO.setIdCard ("370826198901133299"); userDTO.setAddress ("East Railway Station of Jianggan District, Hangzhou City, Zhejiang Province"); String s = JSON.toJSONString (userDTO,new FastjsonDesensitizeFilter ()); System.out.println (s) } public static void testJackson () throws JsonProcessingException {UserDTO userDTO=new UserDTO (); userDTO.setUsername ("Zhang San"); userDTO.setPhone ("15669057552"); userDTO.setIdCard ("370826198901133299"); userDTO.setAddress ("East Railway Station of Jianggan District, Hangzhou City, Zhejiang Province"); ObjectMapper objectMapper = new ObjectMapper (); String value = objectMapper.writeValueAsString (userDTO); System.out.println (value);}
Final output result
{"idCard": "3708 railway stations 3299", "username": "Zhang *", "phone": "156 million railway stations 7552", "address": "East Railway Station of Jianggan District, Hangzhou City, Zhejiang Province"} this is the end of the study on "how to implement jackson based on serialization". Hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.