Generic method won't work - java

My generic method gives casting exception.
java.util.LinkedHashMap cannot be cast to com.example.model.Student
main class
public static void main(String[] args) {
Map<String, Student> students = SampleClass.getStudents("students.json");
System.out.println(students.get("student1").getName());
}
SampleClass
public static <T> Map<String, T> getStudents(String filename) {
Map<String, T> studentMap = null;
ObjectMapper mapper = new ObjectMapper();
try {
studentMap = mapper.readValue(new File(filename),
new TypeReference<Map<String, T>>() { });
} catch (JsonParseException e) {
System.out.println(e.getMessage());
} catch (JsonMappingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return (Map<String, T>)studentMap;
}
what I'm doing wrong?
UPDATE
json file
{
"student1" :
{
"name" : "name1",
"age" : 22
},
"student2" :
{
"name" : "name2",
"age" : 22
}
}
UPDATE
found this as solution.

The problem is with your TypeReference<Map<String, T>>. When you give T here jackson will try to infer the type. Since at this point jackson doesnt know anything about Student class it infer the "name" : "name1" to be a LinkedHashMap with key "name" and value "name1". So in-turn it creates a LinkedHashMap<String, LinkedHashMap<String>>.
As a quick solution you in the object mapper method you could use TypeReference as
studentMap = mapper.readValue(new File(filename), new TypeReference<Map<String, Student>>() { });
Since the method is getStudents it make sense to use this TypeReference but then the whole point of generics in that method is wasted.
Another approach would be to use a custom deserializer. There are many ways of doing this. You could find the details in http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
For example lets make a marker interface for all possible class use a custom deserializer. Say for you scenario lets say we have an interface StudentI which will be implemented by all possible classes you type T could have. Then use the #JsonTypeInfo to give additional details about the class
Student interface
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
#JsonSubTypes({
#Type(value = Student.class, name = "student") })
public interface StudentI {
}
This means if you give something like 'type : student' in your json the mapper would use Student.class for you T.
Sample Class
import java.io.File;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SampleClass {
public static <T extends StudentI> Map<String, T> getStudents(String filename) {
Map<String, T> studentMap = null;
ObjectMapper mapper = new ObjectMapper();
try {
studentMap = mapper.readValue(new File(filename),
new TypeReference<Map<String, StudentI>>() { });
} catch (JsonParseException e) {
System.out.println(e.getMessage());
} catch (JsonMappingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return (Map<String, T>)studentMap;
}
}
Students.json
{
"student1" :
{
"name" : "name1",
"age" : 22,
"type" : "student"
},
"student2" :
{
"name" : "name2",
"age" : 22,
"type" : "student"
}
}
Now your Student class should implement this interface
public class Student implements StudentI {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
Once this is done your code would work just as expected
public static void main(String[] args) {
Map<String, Student> students = SampleClass.getStudents("students.json");
System.out.println(students.get("student1").getName());
}
//Output : name1

Depends what's in your json file. Looks like the mapper is returning a Student object rather than a map.

Related

Mapping JSON response to different types

I'm using Spring 2.6 and we make a GET request via
restTemplate.exchange(url, HttpMethod.GET, httpEntity, ResponseType.class).getBody();
The JSON response can be of two kinds:
1st:
public class ResponseType {
private String data;
}
2nd:
public class ResponseType {
private Subclass data;
}
public class Subclass {
private String classId;
private String detail;
}
In the first version I only get a reference link to the subclass resource.
If the URL contains a 'resolve' flag, than the reference link get expanded already in the first request.
The classId then also specifies what kind of class it is ( 'a.b.c' or 'x.y.z' )
No problem for JSON, but how can I get a mapping in Java?
When having more fields being dynamic (link or instance based on classId) a manual way would be difficult to implement if the combination could be 2 links and 3 objects.
It also could be that a object has the same feature - a filed with a link or a instance of a class specified by classId.
The JSON response would be this:
{
"data": "abskasdkjhkjsahfkajdf-linkToResource"
}
or this:
{
"data": {
"classId": "a.b.subclass",
"detail": "some data"
}
}
or this:
{
"data": {
"classId": "a.b.subclass",
"detail": "some data"
"data2": "some-link-id",
"data3": {
"detailB": "foo",
"detailC": "some-link-id"
}
}
}
Here I do have a possible solution for my problem. The logic to print the address only or the POJO relies soley in the CustomItemSerializer. So it is possible to use this without using duplicate code in controllers.
package com.allianz.clana.datamodel.http.epc.test;
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.Data;
import lombok.NoArgsConstructor;
public class JacksonTester2 {
public static void main(String args[]) throws ParseException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Item item2 = new Item("link");
Stuff stuff = new Stuff();
stuff.setItem(item2);
stuff.setFoo("foo");
String jsonStringStuff = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(stuff);
System.out.println(jsonStringStuff);
Item item3 = new Item("{ \"name\":\"ID3\", \"creationDate\":\"1984-12-30\", \"rollNo\": 1 }");
stuff.setItem(item3);
stuff.setFoo("bar");
jsonStringStuff = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(stuff);
System.out.println(jsonStringStuff);
}
}
class CustomItemSerializer extends StdSerializer<Item> {
private static final long serialVersionUID = 1L;
public CustomItemSerializer() {
this(null);
}
public CustomItemSerializer(Class<Item> t) {
super(t);
}
#Override
public void serialize(Item item, JsonGenerator generator, SerializerProvider arg2) throws IOException {
if (item != null) {
if (item.getItem() != null) {
System.out.println("ItemA POJO data");
generator.writePOJO(item.getItem());
} else {
System.out.println("raw data with link");
generator.writeString(item.getRawdata());
}
}
}
}
#Data
class Stuff {
Item item;
String foo;
}
#JsonSerialize(using = CustomItemSerializer.class)
#Data
#NoArgsConstructor
class Item {
private String rawdata;
#JsonIgnore
private ItemA item;
public Item(String rawdata) {
this.rawdata = rawdata;
if (rawdata.contains("{")) {
try {
this.item = new ObjectMapper().readerFor(ItemA.class).readValue(rawdata);
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
}
#Data
#NoArgsConstructor
class ItemA{
private String name;
private int rollNo;
private String creationDate;
public ItemA(String name, int rollNo, String dob) {
this.name = name;
this.rollNo = rollNo;
this.creationDate = dob;
}
}
The output looks like this:
raw data with link
{
"item" : "link",
"foo" : "foo"
}
ItemA POJO data
{
"item" : {
"name" : "ID3",
"rollNo" : 1,
"creationDate" : "1984-12-30"
},
"foo" : "bar"
}
The CustomItemSerializer decides if the link is printed or the POJO.

Jackson skip field when creating JSON [duplicate]

This question already has answers here:
What is use of the annotation #JsonIgnore?
(3 answers)
Closed 5 months ago.
I have a class like
public class MyPojo {
String name,
String age
String sub
}
And map like
map("name":"john","age":21)
Using Jacksons ObjectMapper, I get a string like
{
"name": "john",
"age": "21",
"sub": null
}
but instead I want to exclude the sub:
{
"name": "john",
"age": "21"
}
How can I do that and tell Jackson to skip sub?
P.S. Please keep in mind that I want to have ability to exclude age and include sub without changing the POJO class, so #JsonIgnore doesn't quite fit.
You can use java.util.Optional in your POJO class. You can convert Map to POJO and after that serialise it ignoring null-s. Optional allows to distinguish map.put("property", null) from not setting property at all. See below example:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class DateApp {
private final static JsonMapper JSON_MAPPER = JsonMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.addModule(new Jdk8Module())
.build();
public static void main(String[] args) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "John");
map.put("age", 21);
MyPojo pojo = JSON_MAPPER.convertValue(map, MyPojo.class);
System.out.println(pojo);
System.out.println("JSON:");
JSON_MAPPER.writeValue(System.out, pojo);
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor
#JsonInclude(JsonInclude.Include.NON_NULL)
class MyPojo {
private Optional<String> name;
private Optional<String> age;
private Optional<String> sub;
}
Above code prints:
MyPojo(name=Optional[John], age=Optional[21], sub=null)
JSON:
{
"name" : "John",
"age" : "21"
}
You can try this approach in order to avoid the null attributes in the final json.
I have used ObjectMapper object and set the below property to avoid null attributes in the json.
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Code as follows:
MyPojo.java
public class MyPojo {
private String name;
private String age;
private String sub;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
#Override
public String toString() {
return "MyPojo{" +
"name=" + name +
", age=" + age +
", sub=" + sub +
'}';
}
}
Test.java
public class Test {
public static void main(String[] args) throws JsonProcessingException {
Map<String,String> inputMap = new HashMap<>();
inputMap.put("age","21");
inputMap.put("name","John");
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MyPojo p = mapper.convertValue(inputMap,MyPojo.class);
System.out.println(p);
System.out.println(mapper.writeValueAsString(p));
}
}
Output:
MyPojo{name=John, age=21, sub=null}
{"name":"John","age":"21"}
You can create your custom serializer.
Just include your map in the serializer code
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
#JsonSerialize(using = MyPojoSerializer.class)
public class MyPojo {
String name;
String age;
String sub;
}
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class MyPojoSerializer extends JsonSerializer<MyPojo> {
#Override
public void serialize(MyPojo myPojo, JsonGenerator jGen, SerializerProvider serializerProvider) throws IOException {
jGen.writeStartObject();
// Map map = ....
for (final Field field : myPojo.getClass().getDeclaredFields()) {
ReflectionUtils.makeAccessible(field);
final String fieldName = field.getName();
final Object fieldValue = ReflectionUtils.getField(field, myPojo);
if (map.containsKey(fieldName)) {
jGen.writeFieldName(fieldName);
jGen.writeObject(fieldValue);
}
}
jGen.writeEndObject();
}
}

Convert JSONArray to Map using Jackson

In our legacy code, we are sending request body in a form of a HashMap, something I cannot change because other applications may be affected.
Sample Hashmap value = {name=name1, age=age1}
However I have problem on using HashMap if there are multiple JSON objects in my request, for example
HashMap<String, Object> map = new HashMap<String, Object>();
for (Person person: Persons) {
map.put("name", "name1");
map.put("age", "age1");
}
If there are 2 or more people, only the last person's name and age will be put in the Map, because the first person's name and age are overridden because they have the same key ("name" and "age").
My desired map value was [{name=name1, age=age1}, {name=name2, age=age2}], but I only got {name=name2, age=age2}
What I did is, in every loop, I put it in JSONArray:
JSONArray jsonArray = new jsonArray();
for (Person person: Persons) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "name1");
map.put("age", "age1");
jsonArray.put(map);
}
So when I print JSONArray, it is:
[{"name":"name1", "age":"age1"}, {"name":"name2", "age":"age2"}]
But again, I need to transform this into HashMap so I can pass it as parameter to oursendRequest() method.
I tried to use the ObjectMapper in Jackson, but it didn't work.
Is this possible, and how?
I would deserialize the JSON into a List of people first. After that, I would group them by name.
Main.java
package q61078696;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
try {
List<Person> people = loadJSON("q61078696/people.json", Person.class);
Map<String, List<Person>> groupedByName = people.stream()
.collect(Collectors.groupingBy(Person::getName));
System.out.println(groupedByName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String loadJSON(String resourceName) {
InputStream is = Main.class.getClassLoader().getResourceAsStream(resourceName);
String jsonString = null;
try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
jsonString = scanner.useDelimiter("\\A").next();
}
return jsonString;
}
public static <E> List<E> loadJSON(String resourceName, Class<E> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = loadJSON(resourceName);
CollectionType typeReference = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz);
return mapper.readValue(jsonString, typeReference);
}
}
Output
{Tom=[{ "name": "Tom", "age": 28 }], Bob=[{ "name": "Bob", "age": 42 }, { "name": "Bob", "age": 21 }], Mary=[{ "name": "Mary", "age": 35 }]}
Person.java
package q61078696;
public class Person {
private String name;
private int age;
public Person() {
this(null, 0);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public String toString() {
return String.format("{ \"name\": \"%s\", \"age\": %d }", this.name, this.age);
}
}
people.json
[
{ "name" : "Bob", "age" : 42 },
{ "name" : "Bob", "age" : 21 },
{ "name" : "Mary", "age" : 35 },
{ "name" : "Tom", "age" : 28 }
]
Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
Additional Info
If you want to map by name and avoid grouping (ignoring dupes).
This will throw an IllegalStateException, because there are duplicate keys, to avoid this, you will need to access the map.
Map<String, Person> groupedByName = people.stream()
.collect(Collectors.toMap(Person::getName, Function.identity()));
You can avoid this by specifying a mergeFunction
Map<String, Person> groupedByName = people.stream()
.collect(Collectors.toMap(
Person::getName, // keyMapper
Function.identity(), // valueMapper
(o1, o2) -> o1, // mergeFunction (keep the first occurrence)
TreeMap::new) // mapSupplier
);
You can also specify a supplier in the 4th parameter. I chose a TreeMap to keep the name keys in order.
See: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toMap-java.util.function.Function-java.util.function.Function-java.util.function.BinaryOperator-

Changing name while assigning and getting value from and to json - Objectmapper java

While assigning values from DTO to different entity names, I assigned correctly with the help of JsonProperty. FrontEnd expecting in a different name. Values for DTO object will get from different object. That I have to assign to entity. Instead of using plain java and copying, am using objectmapper. Here then entity values will be used by frontend. How to print the entity values in different name? Please check below code.
//DTO Class
import java.util.List;
public class StaffDTO {
private String nameDT;
private List<String> skillDT;
//Getter and Setters
}
//Entity Class
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({
"nameDT",
"skillDT"
})
public class Staff {
#JsonProperty("nameDT")
private String name;
#JsonProperty("skillDT")
private List<String> skills;
//Getter and Setters
}
//Call Method
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SeatMapCall1 {
public static void main(String[] args) {
try {
StaffDTO staffDTO = createDummyObject();
System.out.println(convertObjectToJson(staffDTO));
Staff staff= convertJsonToObject(convertObjectToJson(staffDTO),Staff.class);
System.out.println(convertObjectToJson(staff));
} catch (Throwable e) {
e.printStackTrace();
}
}
private static <T> T convertJsonToObject(String jsonStrRes,
Class<T> classArg) {
T resObj = null;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
resObj = mapper.readValue(jsonStrRes, classArg);
} catch (Throwable e) {
e.printStackTrace();
}
return resObj;
}
public static <T> String convertObjectToJson(T obj) {
String jsonStringReq = null;
ObjectMapper objMapper = new ObjectMapper();
try {
jsonStringReq = objMapper.writeValueAsString(obj);
} catch (Exception e) {
}
return jsonStringReq;
}
private static StaffDTO createDummyObject() {
StaffDTO staffDTO = new StaffDTO();
staffDTO.setNameDT("mkyong");
List<String> skills = new ArrayList<>();
skills.add("java");
skills.add("python");
staffDTO.setSkillDT(skills);
return staffDTO;
}
}
//Displays output as
{"nameDT":"mkyong","skillDT":["java","python"]}
{"nameDT":"mkyong","skillDT":["java","python"]}
But I want
{"nameDT":"mkyong","skillDT":["java","python"]}
{"name":"mkyong","skills":["java","python"]}
If I use the below getter and setters in Staff class, I am getting expected as below
{"nameDT":"mkyong","skillDT":["java","python"]}
{"nameDT":"mkyong","skillDT":["java","python"],"name":"mkyong","skills":["java","python"]}
Here it includes both nameDT, skillDT and name, skills. I don't need nameDT, skillDT.
#JsonProperty("nameDT")
private String nameDT;
#JsonProperty("skillDT")
private List<String> skillDT;
public String getName()
{ return nameDT; }
public void setName(String nameDT)
{ this.nameDT = nameDT; }
public List<String> getSkill()
{ return skillDT; }
public void setSkill(List<String> skillDT)
{ this.skillDT = skillDT; }

Mapping some HashMap Key/Values to a POJO

I have a HashMap of about 300 Key/String Value pairs and a POJO with about 12 string attributes where the names match the key names.
I would like to know how to get the HashMap values into the POJO?
I made this start which uses relfection and a loop but wasn't sure how to dynamically construct the setter method name, and apparently reflection is a bad idea anyway...but FWIW:
public void writeToFile(Map<String, String> currentSale) throws IOException {
SaleExport saleExport = new SaleExport();
Field[] fields = saleExport.getClass().getDeclaredFields();
for (Field field : fields ) {
System.out.println(field.getName());
saleExport.set +field(saleExport.get(field));
I have used map struct once before but it does not appear to support HashMaps.
UPDATE
This answer looks similar to what I want to do but gave a stack error on fields that didn't map:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: Unrecognized field "Physical" (class com.SaleExport), not marked as ignorable (6 known properties: "date", "city", "surname", "streetName", "salesNo", "salesSurname"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.SalesExport["Physical"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3738)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3656)
at com.CSVExport.writeToFile(CSVExport.java:20)
at com.JFrameTest.writefiletoDB(JFrameTest.java:135)
at com.JFrameTest$FileWorkerThread.run(JFrameTest.java:947)
To ignore the errors I tried :
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
But then nothing got mapped.
If i have understood your question correctly - you want to put the value of map into the member variable of the Pojo based on key.
Try below approach.
Main Class as follows
package org.anuj.collections.map;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ConverMapToPojo {
public static void main(String[] args) {
Map<String, String> map = getMap();
Set<String> keySet = map.keySet();
String fieldName = null;
Pojo pojo = new Pojo();
Field[] field = Pojo.class.getDeclaredFields();
for (Field f : field) {
fieldName = f.getName();
if (keySet.contains(fieldName)) {
pojo = setField(fieldName, map.get(fieldName), pojo);
}
}
System.out.println("fName = " + pojo.getfName());
System.out.println("lName = " + pojo.getlName());
}
private static Pojo setField(String fieldName, String value, Pojo pojo) {
switch (fieldName) {
case "fName":
pojo.setfName(value);
break;
case "lName":
pojo.setlName(value);
break;
}
return pojo;
}
private static Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("fName", "stack");
map.put("lName", "overflow");
return map;
}
}
Pojo Class as follows -
public class Pojo {
private String fName;
private String lName;
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
}
The Result comes out to be
fName = stack
lName = overflow
Try using Dozer mapping to map HashMap to POJO.You can look at MapStruct too.
Hi I don't know if it's a good Idea but you could convert your map to a json and convert json to your POJO.
You could use gson.
You can inject something in a pojo member variable through e.g. a method like this. I do not say that it is a good way to do this, but it can be done like this.
import java.lang.reflect.Field;
import java.util.HashMap;
/**
* #author Ivo Woltring
*/
public class HashMapKeyStuff {
/**
* The most simple cdi like method.
*
* #param injectable the object you want to inject something in
* #param fieldname the fieldname to inject to
* #param value the value to assign to the fieldname
*/
public static void injectField(final Object injectable, final String fieldname, final Object value) {
try {
final Field field = injectable.getClass()
.getDeclaredField(fieldname);
final boolean origionalValue = field.isAccessible();
field.setAccessible(true);
field.set(injectable, value);
field.setAccessible(origionalValue);
} catch (final NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
private void doIt() {
HashMap<String, String> foo = new HashMap<>();
foo.put("hello", "world");
foo.put("message", "You are great");
MyPOJO pojo = new MyPOJO();
for (final String key : foo.keySet()) {
injectField(pojo, key, foo.get(key));
}
System.out.println("pojo = " + pojo);
}
public static void main(String[] args) {
new HashMapKeyStuff().doIt();
}
}
class MyPOJO {
private String hello;
private String message;
public String getHello() {
return this.hello;
}
public String getMessage() {
return this.message;
}
#Override
public String toString() {
return "MyPOJO{" +
"hello='" + hello + '\'' +
", message='" + message + '\'' +
'}';
}
}
I think you had the right idea in your code. I understand your POJO to contain only a subset of the fields represented in the HashMap, so you just iterate through the fields, populating them from the HashMap as you find them.
public SaleExport toSaleExport(Map<String,String> currentSale) {
SaleExport saleExport=new SaleExport();
Field[] fields=SaleExport.class.getDeclaredFields();
for (Field field : fields) {
String name=field.getName();
if (currentSale.containsKey(name)) {
field.set(saleExport, currentSale.get(name));
}
}
return saleExport;
}

Categories