Java Gson序列化某些环境中的名称更改
我有一个带有GSon注释的DTO。Java Gson序列化某些环境中的名称更改,java,spring-boot,gson,Java,Spring Boot,Gson,我有一个带有GSon注释的DTO。 我的问题是,如果我的应用程序运行在开发阶段、暂存阶段或生产阶段,这些注释的值必须更改… 目前,我必须用不同的值打包我的应用程序,我希望这是自动的。。。它位于Spring启动应用程序中,我想使用Spring.profiles.active告诉我的应用程序使用正确的serializedName 以下是我使用的代码类型 // Tests // @SerializedName("customfield_10123") // Prod @Serializ
我的问题是,如果我的应用程序运行在开发阶段、暂存阶段或生产阶段,这些注释的值必须更改…
目前,我必须用不同的值打包我的应用程序,我希望这是自动的。。。它位于Spring启动应用程序中,我想使用
Spring.profiles.active
告诉我的应用程序使用正确的serializedName
以下是我使用的代码类型
// Tests
// @SerializedName("customfield_10123")
// Prod
@SerializedName("customfield_10114")
private ActionDto action;
我希望有更好的方法来做这件事?下面是一个非常粗糙的例子,说明如何实现您的目标: 首先为每个可能的配置文件创建一个属性文件(名称可以是任何内容,但配置文件必须位于名称上): 根据每个配置文件,使用每个键所需的值填充属性:
test=abc.test
...
注释您的POJO:
public class Foo {
@SerializedName("${test}")
private String name;
...
}
为类创建自定义序列化程序,该程序将解释自定义名称,如下所示:
public class FooSerializer implements JsonSerializer<Foo> {
private static final Pattern PATTERN = Pattern.compile("\\$\\{(.*)\\}");
private static Properties props;
static {
try {
Resource resource = new ClassPathResource(String.format("/application-%s.properties", System.getProperty("spring.profiles.active")));
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public JsonElement serialize(Foo foo, Type type, JsonSerializationContext jsonSerializationContext) {
Field[] fields = foo.getClass().getDeclaredFields();
JsonObject object = new JsonObject();
for (Field field : fields) {
field.setAccessible(true);
String name = field.getName();
if (field.isAnnotationPresent(SerializedName.class)) {
String value = field.getAnnotation(SerializedName.class).value();
Matcher matcher = PATTERN.matcher(value);
if (matcher.find()) {
name = props.get(matcher.group(1)).toString();
} else {
name = value;
}
}
try {
if (field.get(foo) != null) {
object.addProperty(name, field.get(foo).toString());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return object;
}
}
当然,可能有更好的方法根据活动概要文件恢复属性文件,但是给定的代码片段应该足以让您继续。此外,您需要考虑的事实是,在任何给定时间都可能有多个配置文件,因此,如果这是您的场景,则需要在恢复属性之前考虑它。
如果总是希望使用属性中的值,则甚至不需要正则表达式部分。我使用正则表达式来允许这两种情况
如果有什么不清楚的地方,请告诉我,我会努力改进
编辑:
对于反序列化,我无法想出任何非常好的方法,因此这里有一个示例,我认为它远远不够好,但可以完成工作:
功能接口:
public interface Converter {
Object convert(String s);
}
反序列化程序:
public class FooDeserializer implements JsonDeserializer<Foo> {
private static final Pattern PATTERN = Pattern.compile("\\$\\{(.*)\\}");
private static Properties props;
private static Map<Class, Converter> converterForClass = new HashMap<>();
static {
try {
Resource resource = new ClassPathResource(String.format("/application-%s.properties", System.getProperty("spring.profiles.active")));
props = PropertiesLoaderUtils.loadProperties(resource);
converterForClass.put(Integer.TYPE, s -> Integer.parseInt(s.replace("\"", "")));
converterForClass.put(Double.TYPE, s -> Double.parseDouble(s.replace("\"", "")));
converterForClass.put(String.class, s -> s);
converterForClass.put(Long.TYPE, s -> Long.parseLong(s.replace("\"", "")));
converterForClass.put(Boolean.TYPE, s -> Boolean.parseBoolean(s.replace("\"", "")));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Foo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Foo foo = new Foo();
JsonObject jobject = (JsonObject) jsonElement;
for (Entry entry : jobject.entrySet()) {
Field field = searchField(entry.getKey().toString());
if (field != null) {
field.setAccessible(true);
try {
Object r = converterForClass.get(field.getType()).convert(entry.getValue().toString());
field.set(foo, r);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return foo;
}
private Field searchField(String name) {
Field[] fields = Foo.class.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.isAnnotationPresent(SerializedName.class)) {
String value = field.getAnnotation(SerializedName.class).value();
Matcher matcher = PATTERN.matcher(value);
if (value.equals(name)) {
return field;
} else if (matcher.find()) {
if (props.get(matcher.group(1)).equals(name)) {
return field;
}
}
} else {
if (field.getName().equals(name)) {
return field;
}
}
}
return null;
}
上述方法的问题在于,它无法处理嵌套对象。您将需要进行一些进一步的验证和实施。它还使用Java 8功能。反序列化有点困难,因为每种类型都有问题。我将不得不想一想如何简化它。我应该尽快发布一些东西。只是想让你知道,在我的应用程序中,我需要一些属性文件中的字段,其他字段在所有环境中都具有相同的名称。所以正则表达式是一个非常好的选择that@bryce不幸的是,我想不出什么好东西。我只能想出一个手动检查类型的方法。好的,谢谢。我将使用您的序列化解决方案,并尝试找到另一个反序列化解决方案;-)
public interface Converter {
Object convert(String s);
}
public class FooDeserializer implements JsonDeserializer<Foo> {
private static final Pattern PATTERN = Pattern.compile("\\$\\{(.*)\\}");
private static Properties props;
private static Map<Class, Converter> converterForClass = new HashMap<>();
static {
try {
Resource resource = new ClassPathResource(String.format("/application-%s.properties", System.getProperty("spring.profiles.active")));
props = PropertiesLoaderUtils.loadProperties(resource);
converterForClass.put(Integer.TYPE, s -> Integer.parseInt(s.replace("\"", "")));
converterForClass.put(Double.TYPE, s -> Double.parseDouble(s.replace("\"", "")));
converterForClass.put(String.class, s -> s);
converterForClass.put(Long.TYPE, s -> Long.parseLong(s.replace("\"", "")));
converterForClass.put(Boolean.TYPE, s -> Boolean.parseBoolean(s.replace("\"", "")));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Foo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Foo foo = new Foo();
JsonObject jobject = (JsonObject) jsonElement;
for (Entry entry : jobject.entrySet()) {
Field field = searchField(entry.getKey().toString());
if (field != null) {
field.setAccessible(true);
try {
Object r = converterForClass.get(field.getType()).convert(entry.getValue().toString());
field.set(foo, r);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return foo;
}
private Field searchField(String name) {
Field[] fields = Foo.class.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.isAnnotationPresent(SerializedName.class)) {
String value = field.getAnnotation(SerializedName.class).value();
Matcher matcher = PATTERN.matcher(value);
if (value.equals(name)) {
return field;
} else if (matcher.find()) {
if (props.get(matcher.group(1)).equals(name)) {
return field;
}
}
} else {
if (field.getName().equals(name)) {
return field;
}
}
}
return null;
}
gsonBuilder.registerTypeAdapter(Foo.class, new FooDeserializer());