In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the conversion of custom enumeration gson". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
CodedTypeTypeAdapterimport com.google.gson.*;import com.utils.mybatis.CodedEnum;import java.lang.reflect.Type / * * CodedEnum conversion rules in GSON, using code instead of the characters * * @ param * @ author tenmao * / public class CodedTypeTypeAdapter implements JsonSerializer, JsonDeserializer {@ Override public E deserialize (JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {if (type instanceof Class) {@ SuppressWarnings ("unchecked") Class klass = (Class) type; return CodedEnum.codeOf (klass, jsonElement.getAsInt ()) .orElse (null) } else {throw new RuntimeException (String.format ("json% s cannot convert to type s", jsonElement, type));} @ Override public JsonElement serialize (E, Type type, JsonSerializationContext jsonSerializationContext) {return new JsonPrimitive (e.getCode ());}} HttpMessageConverterimport com.google.gson.GsonBuilder;import com.tenmao.utils.mybatis.CodedEnum;import com.tenmao.utils.mybatis.converter.CodedTypeTypeAdapter;import lombok.extern.slf4j.Slf4j Import org.reflections.Reflections;import org.reflections.scanners.SubTypesScanner;import org.springframework.http.converter.json.GsonHttpMessageConverter;import java.util.Set;/** * @ author tenmao * @ since 2017-12-1 * / @ Slf4jpublic class HttpMessageConverter extends GsonHttpMessageConverter {public HttpMessageConverter () {final GsonBuilder builder = new GsonBuilder (); final Reflections reflections = new Reflections ("com.tenmao", new SubTypesScanner (true)) Final Set allClasses = reflections.getStore (). GetSubTypesOf (CodedEnum.class.getName ()); for (String klass: allClasses) {try {final Class aClass = Class.forName (klass); builder.registerTypeAdapter (aClass, new CodedTypeTypeAdapter ());} catch (ClassNotFoundException e) {log.error ("fail to register for gson", e) }} setGson (builder.create ());}} spring-mvc.xml complete
After implementing the above steps, as long as you implement the custom enumeration of interface CodedEnum, you can automatically convert to its code value.
This is the end of the conversion of Custom enumeration gson. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.