I have a DTO with some GSon annotation.
My probleme is that the value of these annotations have to change if my application run in developpement or in staging or in production...
For the moment, I have to package my application with the different value and I want this to be automatic... It is in a Spring Boot application and I want to use the spring.profiles.active to tell my application to take the right serializedName
Here is the kind of code I use
// Tests
// #SerializedName("customfield_10123")
// Prod
#SerializedName("customfield_10114")
private ActionDto action;
I hope there is a better way to do it?
Here is a very crude example on how you can achieve what you want:
First create a propery file for each possible profile (name can be anything, but the profile must be on the name):
application-dev.properties
application-prod.properties
...
Populate the properties with the values you want for each key accordingly to each profile:
test=abc.test
...
Annotate your POJOs:
public class Foo {
#SerializedName("${test}")
private String name;
...
}
Create a custom serializer for your class, which will interpret the custom names, something like this:
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;
}
}
Now you just need to register your custom serializer and you are good to go:
Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class, new FooSerializer()).setPrettyPrinting().create();
Of course there may be better ways to recover the properties file according to the active profile, but the given snippet should be enough to get you going. Also, you need to consider the fact that there may be multiple profiles active at any given time, so if that is your scenario, you need to take it into consideration before recovering the properties.
You don't even need the regex part if you will always want to use the value from the properties. I used a regex to allow both cases.
If something wasn't clear, please let me know and I will try to improve it.
EDIT:
For the deserialization I can't come up with anything very good, so here is an example which I think is far from OK, but gets the job done:
Functional interface:
public interface Converter {
Object convert(String s);
}
Deserializer:
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;
}
Register the deserializer:
gsonBuilder.registerTypeAdapter(Foo.class, new FooDeserializer());
The problem with the above approach is it will NOT work with nested objects. You will have to some further verifications and implementation. It is using Java 8 features as well.
Related
Imagine you have a model class hierarchy, like
public class TopLevel {
private MiddleLevel middleLevel = null;
public TopLevel() {
middleLevel = new MiddleLevel();
}
public MiddleLevel getMiddleLevel() { return middleLevel; }
}
public class MiddleLevel {
private LowLevel lowLevel = null;
public MiddleLevel () {
lowLevel = new LowLevel();
}
public LowLevel getLowLevel() { return lowLevel; }
}
public class LowLevel {
private Value value = null;
public LowLevel() {
value = new Value();
}
public Value getValue() { return value; }
}
public class Value {
private String stringValue = "ItsAValue";
private String doubleValue = 1.0d;
private String integerValue = 4321;
public void setStringValue(String value) {
stringValue = value;
}
}
And of course further classes with different attributes. E.g. this hierarchy was created and instantiated by Jaxb.
Now, i want to set a value in the Value-class. Of course i can execute something like:
TopLevel topLevel = new TopLevel();
topLevel.getMiddleLevel().getLowLevel().getValue().setStringValue("NewValue");
Is there a way to simplify or to generalize this, e.g. to be able to call the "path" through all these class-objects to set a value deep inside? Here is some pseudocode, what i mean:
public class Anotherclass {
public static void main(String[] args) {
TopLevel topLevel = new TopLevel();
setStringValueByPath("topLevel/middleLevel/lowLevel/value/stringValue", "newValue");
setDoubleValueByPath("topLevel/middleLevel/lowLevel/value/doubleValue", 5.0d);
setIntegerValueByPath("topLevel/middleLevel/lowLevel/value/integerValue", 1234);
}
}
Thanks a lot
Alex
Ok, if anyone is interested, i think i found a solution, that i was looking for:
A recursive approach based on Java.reflection :
public class ReflectionSetter {
private static List<Field> getFields(Object object) {
List<Field> fields = new ArrayList<>();
fields.addAll(Arrays.asList(object.getClass().getDeclaredFields()));
return fields;
}
private static Field hasField(Object object, String fieldName) {
for (Field f : getFields(object)) {
if (f.getName().equalsIgnoreCase(fieldName)) return f;
}
return null;
}
public static void setValue(Object object, String path, String newValue) throws IllegalArgumentException, IllegalAccessException {
if (path.contains("/")) {
int pos = path.indexOf('/');
String first = path.substring(0, pos);
String rest = path.substring(pos+1);
Field f = ReflectionSetter.hasField(object, first);
if (null == f) throw new IllegalArgumentException("Path not found: " + path);
f.setAccessible(true);
Object obj = f.get(object);
setValue(obj, rest, newValue);
} else {
Field f = ReflectionSetter.hasField(object, path);
if (f == null) throw new IllegalArgumentException("Field not found: " + path);
// if found -> set value
f.setAccessible(true);
f.set(object, newValue);
}
}
}
Now, you can set a value via a path. Usage:
TopLevel topLevel = new TopLevel();
ReflectionSetter.setValue(topLevel, "middleLevel/lowLevel/value/myValue", "NewValue");
An ideal efficient way to do this and by focussing more on reducing code complexity and at the same time improving code readability, you should look at design patterns, may be something like visitor pattern.
One of the most common use cases of visitor pattern is and as a result of separating algorithm and the data structure, comes with ability to add new operations to existing object structures without modifying said structures.
Moving on to a phase where "No, I want to look at string based approached as pointed in question". Apache commons library provides something called JxPath.
Unsure if you tried looking at JxPath ref.apache.jx.path
It offers simple interpreter of an expression language called XPath. JXPath applies XPath expressions to graphs of objects of all kinds
Picking an example from your question :
TopLevel topLevel = new TopLevel();
JXPathContext context = JXPathContext.newContext(topLevel);
context.setValue("middleLevel/lowLevel/value/stringValue", "newStringValue");
I need to get the enum name based on value. I am given with enum class and value and need to pick the corresponding name during run time .
I have a class called Information as below.
class Information {
private String value;
private String type;
private String cValue;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getcValue() {
return cValue;
}
public void setcValue(String cValue) {
this.cValue = cValue;
}
public static void main(String args[]) {
Information inf = new Information();
inf.setType("com.abc.SignalsEnum");
inf.setValue("1");
}
}
class SignalEnum {
RED("1"), GREEN("2"), ORANGE("3");
private String sign;
SignalEnum(String pattern) {
this.sign = pattern;
}
}
class MobileEnum {
SAMSUNG("1"), NOKIA("2"), APPLE("3");
private String mobile;
MobileEnum(String mobile) {
this.mobile = mobile;
}
}
In run time i will come to know the enum name using the attribute type from the Information class and also i am getting the value. I need to figure out the corresponding enum to set the value for cValue attribute of Information class.
Just for example i have provided two enums like SignalEnum and MobileEnum but in my actual case i will get one among 100 enum types. Hence i dont want to check type cast. I am looking for some solution using reflection to se the cValue.
Here is a simple resolver for any enum class.
Since reflection operations are expensive, it's better to prepare all required data once and then just query for it.
class EnumResolver {
private Map<String, Enum> map = new ConcurrentHashMap<>();
public EnumResolver(String className) {
try {
Class enumClass = Class.forName(className);
// look for backing property field, e.g. "sign" in SignalEnum
Field accessor = Arrays.stream(enumClass.getDeclaredFields())
.filter(f -> f.getType().equals(String.class))
.findFirst()
.orElseThrow(() -> new NoSuchFieldException("Not found field to access enum backing value"));
accessor.setAccessible(true);
// populate map with pairs like ["1" => SignalEnum.RED, "2" => SignalEnum.GREEN, etc]
for (Enum e : getEnumValues(enumClass)) {
map.put((String) accessor.get(e), e);
}
accessor.setAccessible(false);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public Enum resolve(String backingValue) {
return map.get(backingValue);
}
private <E extends Enum> E[] getEnumValues(Class<E> enumClass) throws ReflectiveOperationException {
Field f = enumClass.getDeclaredField("$VALUES");
f.setAccessible(true);
Object o = f.get(null);
f.setAccessible(false);
return (E[]) o;
}
}
And here is simple JUnit test
public class EnumResolverTest {
#Test
public void testSignalEnum() {
EnumResolver signalResolver = new EnumResolver("com.abc.SignalEnum");
assertEquals(SignalEnum.RED, signalResolver.resolve("1"));
assertEquals(SignalEnum.GREEN, signalResolver.resolve("2"));
assertEquals(SignalEnum.ORANGE, signalResolver.resolve("3"));
}
#Test
public void testMobileEnum() {
EnumResolver mobileResolver = new EnumResolver("com.abc.MobileEnum");
assertEquals(MobileEnum.SAMSUNG, mobileResolver.resolve("1"));
assertEquals(MobileEnum.NOKIA, mobileResolver.resolve("2"));
assertEquals(MobileEnum.APPLE, mobileResolver.resolve("3"));
}
}
And again for performance sake you can also instantiate these various resolvers once and put them into a separate Map
Map<String, EnumResolver> resolverMap = new ConcurrentHashMap<>();
resolverMap.put("com.abc.MobileEnum", new EnumResolver("com.abc.MobileEnum"));
resolverMap.put("com.abc.SignalEnum", new EnumResolver("com.abc.SignalEnum"));
// etc
Information inf = new Information();
inf.setType("com.abc.SignalsEnum");
inf.setValue("1");
SignalEnum red = (SignalEnum) resolverMap.get(inf.getType()).resolve(inf.getValue());
I am using Jackson for de/serialization in my app.
I have a situation where I need to convert a JSON string to one of my 3 classes. In case the string can't be converted to either one of 3 classes, it will considered to be an unrecognized case.
However, if the schema of json string and the provided class in mapper.readValue(jsonString,MyClass1.class) does not match, it throws an UnrecognizedPropertyException.
Currently I am using something like below, but it seems to be pretty messy.
try {
obj = mapper.readValue(jsonString, MyClass1.class);
} catch (UnrecognizedPropertyException e1) {
try {
obj = mapper.readValue(jsonString, MyClass2.class);
} catch (UnrecognizedPropertyException e2) {
try {
obj = mapper.readValue(jsonString, MyClass3.class);
} catch (Exception e) {
//handle unrecognized string
}
} catch (Exception e) {
//handle unrecognized string
}
} catch (Exception e) {
//handle unrecognized string
}
Is this how it needs to be done or is there any other alternative? Is there any way to configure the mapper to return null in case of unrecognized properties, as that would result in creating a simple series if blocks instead of nested try-catch blocks?
You can try this method to do deserialization thing. this will return null on UnrecognizedPropertyException:
private <T> T deserialize(ObjectMapper mapper, Class<T> type, String jsonString) {
T t = null;
try {
t = mapper.readValue(jsonString, type);
} catch (UnrecognizedPropertyException e) {
//handle unrecognized string
}catch (IOException e) {
//handle under errors
}
return t;
}
If jsonString is generated by you, you can consider to add type info and then use it to convert deserialized object. You could refer to this post for how to do it.
If jsonString is generated by other services beyond your control, then there's no type info you can get so you can only try it one by one, #Sachin Gupta's answer would be a nice choice.
I'd like to provide an additional option: define an all-in-one entity including all fields of MyClass1, MyClass2 and MyClass3, and make MyClass1, MyClass2 and MyClass3 be separated wrapper and only expose related fields for each. Code as follows:
Class AllInOne:
public class AllInOne {
protected String a;
protected String b;
protected String c;
public A asA() {
return new A(this);
}
public B asB() {
return new B(this);
}
public C asC() {
return new C(this);
}
}
Class A:
public class A {
private AllInOne allInOne;
public A(AllInOne allInOne) {
this.allInOne = allInOne;
}
public String getA() {
return allInOne.a;
}
}
Class B:
public class B {
private AllInOne allInOne;
public B(AllInOne allInOne) {
this.allInOne = allInOne;
}
public String getB() {
return allInOne.b;
}
}
Class C:
public class C {
private AllInOne allInOne;
public C(AllInOne allInOne) {
this.allInOne = allInOne;
}
public String getC() {
return allInOne.c;
}
}
Test code:
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
String jsonA = "{\"a\":\"a value\"}";
String jsonB = "{\"b\":\"b value\"}";
String jsonC = "{\"c\":\"c value\"}";
needTypeA(om.readValue(jsonA, AllInOne.class).asA());
needTypeB(om.readValue(jsonB, AllInOne.class).asB());
needTypeC(om.readValue(jsonC, AllInOne.class).asC());
}
private static void needTypeA(A a) {
System.out.println(a.getA());
}
private static void needTypeB(B b) {
System.out.println(b.getB());
}
private static void needTypeC(C c) {
System.out.println(c.getC());
}
}
With implementation like this, we erased the specific type info at deserialization step, and bring it back at the moment we really need/use it. And as you can see there's not too much extra code, because what we actually did is just moving all fields declaration together, and added couple methods.
Notes:
I declare fields in AllInOne to be protected, putting all POJO class in the same package will make A, B and C be able to access them directly, but not for other classes outside.
Setting om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); to make jackson deserialize by field, so that we can remove duplicate setter and getter from AllInOne class
If you do need to know the type info, you could add methods like isA inside AllInOne based on the fields info
If json contains some define property, than you can try to use #JsonTypeInfo and #JsonSubTypes. Classes MyClass1, ... must implement this interface. Also I don`t remember exactly how to map unknown implementations to null.
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY, // level of define property
property = <property_name>,
visible = true,
defaultImpl = NoClass.class)
#JsonSubTypes({#JsonSubTypes.Type(value = <interface-impl>.class, name = <property_value>)})
private <interface> value;
// getters and setters
I want to modify a java object by passing a json String to my application. The String will not contain all information about the complete, modified object but merely a single member that's meant to be set.
class SomeClass {
Object var1 = "Hello";
Object var2 = "AAA";
// A lot of fields goes here ...
}
public AppTest() throws Exception {
SomeClass myObject = new SomeClass();
myObject.var2 = "BBB";
String modification = "{\"var1\":\"Goodbye\"}";
Gson gson = new Gson();
SomeClass modifed = gson.fromJson(modification, SomeClass.class);
// TODO: Merge a modifed object into myObject somehow
}
Furthermore, some of the fields might be objects with any number of fields. Again, I might want to just modify a single primitive inside the child object. A more complex example:
class SomeOtherClass {
String var4 = "444";
String var5 = "555";
}
class SomeClass {
Object var1 = "111";
Object var2 = "222";
SomeOtherClass var3 = new SomeOtherClass();
}
public AppTest() throws Exception {
SomeClass myObject = new SomeClass();
myObject.var2 = "AAA";
myObject.var3.var5 = "BBB";
String modification = "{\"var3\":{\"var5\":\"XXX\"}}";
Gson gson = new Gson();
SomeClass modifed = gson.fromJson(modification, SomeClass.class);
// TODO: Merge the modifed object into myObject somehow
}
So, my question is how can I partially modify an object with JSON?
Hi I tried out one pseudo sample as below:
static Object merge(Object target, Object modified) {
for (Field f : target.getClass().getDeclaredFields()) {
if (!f.isAccessible()) {
f.setAccessible(true);
}
if (f.getType().isPrimitive()) {
try {
if (f.getType().isAssignableFrom(java.lang.Boolean.TYPE)
&& f.getBoolean(modified) != f.getBoolean(target)) {
f.setBoolean(target, f.getBoolean(modified));
} else if (f.getType().isAssignableFrom(java.lang.Character.TYPE)
&& f.getChar(modified) != f.getChar(target)) {
f.setChar(target, f.getChar(modified));
} else if (f.getType().isAssignableFrom(java.lang.Integer.TYPE)
&& f.getInt(modified) != f.getInt(target)) {
f.setInt(target, f.getInt(modified));
}
//....
// do it for all other primitive types
//also consider Enum types(not primitive so check 'f.getType().isEnum()')
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else if (f.getType().getPackage().getName().matches("java.lang.*")
|| f.getType().getPackage().getName().matches("java.util.*")) {
/* Here I am trying to directly assign changes for the basic packages, if you want more you can add more packages*/
try {
if (f.get(modified) != null && f.get(target) != f.get(modified)) {
f.set(target, f.get(modified));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
/* If local classes encountered as member variables then do the same merge!*/
try {
merge(f.get(target), f.get(modified));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return target;
}
you can call this method as myObject = (SomeClass) merge(myObject, modifed);
Note: It is not a fully functional method to do your job, you please read the comments inline and make it as a perfect for your case. I only ensure basic functionality
Managed to create a recursive method for replacing properties in a JsonObject.
private static void mergeObjects(JsonObject object, JsonObject modification) {
// Iterate through the modified properties
for (Entry<String, JsonElement> entry : modification.entrySet()) {
JsonElement je = entry.getValue();
// If the modified property is an object, iterate through the properties of the modified property
if (je instanceof JsonObject) {
JsonObject nextOrigObject = object.get(entry.getKey()).getAsJsonObject();
JsonObject nextModObject = je.getAsJsonObject();
mergeObjects(nextOrigObject, nextModObject);
}
// If the modified property is not an object, set the original object to match the modified property
else
object.add(entry.getKey(), je);
}
}
With this method, I can merge two objects as follows:
class SomeClass {
Object var1 = "Hello";
Object var2 = "AAA";
}
public TestApplication() {
SomeClass myObject = new SomeClass();
myObject.var2 = "BBB";
String modificationString = "{\"var1\":\"Goodbye\"}";
Gson gson = new Gson();
JsonObject original = gson.fromJson(gson.toJson(myObject), JsonObject.class);
JsonObject modification = gson.fromJson(modificationString, JsonObject.class);
mergeObjects(original, modification);
myObject = gson.fromJson(original, SomeClass.class);
System.out.println(myObject.var1); // Prints "Goodbye"
}
public static void main(String[] args) {
new DummyFile();
}
Comments?
There might be prettier ways to convert the SomeClass object to a JsonObject before the merging, feel free to add your suggestions.
EDIT: added else in merging method
EDIT2: added comments
Any Problem in doing this
modifed.var2=myObject.var2;
String modificationStr = gson.toJson(modifed);
System.out.println(modificationStr);
I am using spring and hibernate. I have a class (DTO) with a lot of string member variables. I'm trying to implement search for this class. The user should be able to search by each field. I'm using jackson json mapper to serialize and deserialize objects. Is there anyway to identify the fieldName by using JsonProperty value?
Let this be an example: my DTO
public class SampleDTO{
private String field1;
private String field2;
private String field3;
private String field4;
#JsonProperty("FIELD_1")
public String getField1(){
return field1;
}
#JsonProperty("FIELD_2")
public String getField2(){
return field2;
}
#JsonProperty("FIELD_3")
public String getField3(){
return field3;
}
#JsonProperty("FIELD_4")
public String getField4(){
return field4;
}
}
Let this be my search function
public Set<T> search(String fieldName, String searchKeyword) {
String originalFieldName = someMagicFunction(fieldName);
//if fieldName= "FIELD_1", someMagicFunction should return "field1"
Criteria criteria = session.createCriteria(T.class);
criteria.add(Restrictions.eq(originalFieldName, searchKeyword));
return new HashSet<T>(criteria.list());
}
Any implementation is fine. I'm looking for a good approach to handle cases like this. It feels like finding fields manually involves "too much typing".
You basically want to use reflection. There are two possibilities here when it comes to field lookup:
Value of #JsonProperty annotation
Real name of the field
In the first case you may want to use some additional library to ease the pain when using reflection + annotation, but the crude code would look more less like this:
SampleDTO dto = new SampleDTO();
// setup some values here
Field[] fields = r.getClass().getFields();
for(Field f : fields) {
JsonProperty jsonProperty = f.getDeclaredAnnotation(JsonProperty.class);
if (jsonProperty != null && jsonProperty.value().equals("FIELD_1")) {
return (String) f.get(dto);
}
// throw exception since passed field name is illegal
}
In the second one it would be so much easier:
SampleDTO dto = new SampleDTO();
// setup some values here
String field1Value = (String) r.getClass().getField("field1").get(dto);
In case if anyone is interested, this is how I solved the problem. I added this code to DAO's constructor.
try {
BeanInfo beanInfo = Introspector.getBeanInfo(T.class);
Method[] methods = T.class.getMethods();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor propertyDescriptor: propertyDescriptors) {
//I'm looking for string fields only
if (propertyDescriptor.getPropertyType().equals( String.class)) {
//My annotations are on methods
for(Method method: methods) {
if(propertyDescriptor.getReadMethod().equals(method)) {
JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class);
if (jsonProperty != null) {
//jsonFieldMapping is a Map<String,String>
//will be saving the mapping in the format {"FIELD_1":"field1", "FIELD_2":"field2"}
jsonFieldMapping.put(jsonProperty.value(), propertyDescriptor.getDisplayName());
} else {
logger.debug("jsonProperty is null");
}
}
}
}
}
// just printing out the values identified from class
for(String key: jsonFieldMapping.keySet()) {
logger.debug("key: " + key + "value: " + jsonFieldMapping.get(key));
}
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So, my magic method will be
public String getField(String jsonFieldName){
if (jsonFieldMapping.containsKey(jsonFieldName)) {
return jsonFieldMapping.get(jsonFieldName);
} else {
throw new IllegalArgumentException("searching field not found");
}
}
I haven't tested this code completely. Looks like the values in the logs are correct.