I would like to clarify if it is possible to create Map<String, Object> from JPA entity through Hibernate. I mean is it possible to convert persistent object (entity) to Map that contains all entity properties as keys and properties' values as values. I understand that properties can be retrieved through Reflections but I can't figure out how to map it with proper values. I found only one solution and it is to use Spring's JdbcTemplate but it is not an option in my case. If anyone have possible solution please let me know. Thank you in advance.
You could try using #Converter as follows
#Entity
public class SomeEntity{
#Id
//...
#Convert(converter = MyConverter.class)
Map<String,Object> map;
}
and build up your converter as you wish for example convert to/from json
#Converter
public class MyConverter implements
AttributeConverter<Map<String, Object>, String> {
#Override
public String convertToDatabaseColumn(Map<String, Object> map) {
return jsonStr(map);
}
#Override
public Map<String, Object> convertToEntityAttribute(String s) {
return mapFromJson(s);
}
You can access the properties through reflection with the assistance of the Apache BeanUtil library, if your entity follows JavaBean naming conventions for all of its properties.
Step one: Wrap your entity in a WrapDynaBean object.
Step two: Create a DynaBeanPropertyMapDecorator, further wrapping the DynaBean.
Step three: Well, that's it. DynaBeanPropertyMapDecorator implemements Map<String, Object> so your job is done there. getFoo() on your original object may now be found by decorator.get("foo"). (Note that you've lost type-safety here, but you did ask for a Map<String, Object>...)
Related
I am new to Avro serialization. My requirement is to serialize a class having a Map<String, Object> as its property. This object could be any type of class like Student, Teacher, Principal.
class Payload
{
private string transactionNo,
private Map<String,Object> map,
}
Please suggest to me a way to write a schema for the above scenario.
I've been trying to wrap my head around this for hours but it doesn't make much sense what I'm doing wrong. I am trying to create a Map object in Java from a .yml file. Reason being for a map, I don't know what/how many children will appear under "present", so I rather have a dynamic way of creating a map object out of that..
Below is my .yml file. I would like the key-value pair under "present":
present:
now: LOCAL TESTING
later: testing
Below is my config class (everything commented out is what I've tried - in different combinations):
//#Data
#Component
#EnableConfigurationProperties
#ConfigurationProperties(prefix = "present")
//#ConfigurationProperties
public class stat {
//#Getter
//#Data
#Value("${present}")
private Map<String, String> present;
//private Map<String, String> present = new HashMap<String, String>();
}
I tried looking at other SO posts and I feel like I understand it but my Spring Boot (v1.5.8) application isn't seeing that value. It keeps throwing an error for me or the map object is null (or not being populated).
I know that I can read values from this .yml file because if I try to obtain a single value with a code snippet below, it works:
#Data
#Value("${present.now}")
private String status; // String value "LOCAL TESTING"
Here are the other links that I've tried:
Spring Boot yaml configuration for a list of strings
how to convert yml file to java pojo
Am I missing something obvious? Thanks!
You can try to create a POJO to represent the yml structure you are trying to read from.
For example:
#Configuration
#ConfigurationProperties(prefix = "present")
#Data
public class Present {
private String now;
private String later;
}
So I figured it out (for those who have this problem later):
#Value isn't needed and the prefix param in the #ConfigurationProperties isn't needed.
Then you need to have a getter method for the fields that you want - I thought the Lombok library had autogenerated these but I was wrong (probably need to read more about it later - #Setter and #Data won't work properly).
So it should look something like this:
#Component
#EnableConfigurationProperties
#ConfigurationProperties
public class stat {
private Map<String, String[]> present = new HashMap<String, String[]>();
public Map<String, String[]> getPresent() {
return present;
}
}
Now let's give a more complex example (nested maps). Say that my .yml file looks like this:
parent:
present:
foo: dey, tok
bar: ar, jerbs
later:
foo: day, dok
mar: r, darbs
The POJO would look like this:
#Component
#EnableConfigurationProperties
#ConfigurationProperties
public class stat {
private Map<String, Map<String, String[]>> parent = new HashMap<String, Map<String, String[]>>();
public Map<String, Map<String, String[]>> getParent() {
return parent;
}
}
Another key thing to note is that the field that you are obtaining a value from must match the variable name - it may not matter if you use prefix but it still didn't work for me. Hope this helps.
There are several previous questions around using JaxB to marshall/unmarshall a java.util.Map, many of which get pointed back to this example, which works great:
http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html
However, I can't get JaxB to be able to marshall/unmarshall instances of Map if the map is not a member of the #XmlRootElement. For example, here's a root element class,
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public static class Customer {
private MyField myField
MyField getMyField() {
return myField
}
void setMyField(MyField myField) {
this.myField = myField
}
}
The definition of it's field class:
#XmlAccessorType(XmlAccessType.FIELD)
public static class MyField{
Map<String, String> getSomeMap() {
return someMap
}
void setSomeMap(Map<String, String> someMap) {
this.someMap = someMap
}
#XmlElement
private Map<String, String> someMap = new HashMap<String, String>()
}
And some code to drive the marshalling:
JAXBContext jc = JAXBContext.newInstance(Customer.class)
Customer customer = new Customer()
MyField myField1 = new MyField()
myField1.someMap.put("foo", "bar")
myField1.someMap.put("baz", "qux")
customer.myField = myField1
Marshaller marshaller = jc.createMarshaller()
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
marshaller.marshal(customer, System.out)
This example results in:
java.util.Map is an interface, and JAXB can't handle interfaces.
java.util.Map does not have a no-arg default constructor.
I am writing my code in Groovy rather than Java, but I don't think it should make much of a difference.
I was able to encounter the same behavior using JAXB by creating a TestController of type #RestController, using Spring Boot.
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
#RestController
#RequestMapping(value = "test")
class TestController {
#RequestMapping(value = "findList")
List findList() {
["Test1", "Test2", "Test3"] as ArrayList<String>
}
#RequestMapping(value = "findMap")
Map findMap() {
["T1":"Test1", "T2":"Test2", "T3":"Test3"] as HashMap<String,String>
}
#RequestMapping(value = "")
String find(){
"Test Something"
}
}
With JAXB as the default implementation in SpringBoot, I could reproduce the issue that the /test/findList would correctly render XML, but /test/findMap would generate an error as described in the initial posting.
For me, the solution to the problem is to switch the XML rendering library to Jackson (there are others like XStream as well).
Using Gradle for the build file (build.gradle), I simply add the Jackson dependencies, very similar to how you would if using Maven:
'com.fasterxml.jackson.core:jackson-core:2.7.1',
'com.fasterxml.jackson.core:jackson-annotations:2.7.1',
'com.fasterxml.jackson.core:jackson-databind:2.7.1-1',
'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.7.1',
'org.codehaus.woodstox:woodstox-core-asl:4.4.1',
I have experienced this before myself. Bottom line is that the warning is telling you exactly the problem. You have defined your field as type java.util.Map. JAXB does not support interfaces. To correct your problem, you need to change the declaration of your field to a concrete Map type like:
private HashMap<String, String> someMap = new HashMap<String, String>()
Your other option is described in the link you referenced. You need to have a
MapAdapter class as referenced in the link you provided and then include that in the annotation, hinting to JAXB how it should marshal/unmarshal the Map type.
I think this link gives a clearer example of how to create and implement the MapAdapter:
JAXB: how to marshall map into <key>value</key>
The answer to the specific issue I was having ended up being removing the #XmlElement annotation from the Map field like so:
#XmlAccessorType(XmlAccessType.FIELD)
public static class MyField{
Map<String, String> getSomeMap() {
return someMap
}
void setSomeMap(Map<String, String> someMap) {
this.someMap = someMap
}
//#XmlElement Remove this annotation
private Map<String, String> someMap = new HashMap<String, String>()
}
Without that annotation, the marshalling/unmarshalling works fine, and still interprets the Map as an XmlElement - there seems to be a bug with that annotation specifically. However, as #dlcole points out, an alternative (that would allow you to have more control over the format of your serialized representation) is to use Jackson rather than JAXB.
I need to write a JSON string that follows this basic format:
{
"xmlns": {
"nskey1" : "nsurl1",
"nskey2" : "nsurl2"
},
"datakey1": "datavalue1",
"datakey2": "datavalue2"
}
I'm using the following class to present the data, and an instance of this class is serialized with the Jackson ObjectMapper.
public class PayloadData {
public Map<String, String> payloadData = new TreeMap<String, String>();
#JsonProperty("xmlns")
public Map<NamespaceEnum, String> namespaces = new TreeMap<NamespaceEnum, String>();
#JsonAnyGetter
public Map<String, String> getPayloadData() {
return payloadData;
}
}
If I serialize an instance of this class as-is, the resulting JSON will be something like this:
{
"xmlns": {
"nskey1" : "nsurl1",
"nskey2" : "nsurl2"
},
"payloadData": {
"datakey1": "datavalue1",
"datakey2": "datavalue2"
},
"datakey1": "datavalue1",
"datakey2": "datavalue2"
}
That makes sense based on the naming conventions, but I'm looking for a method to have the payloadData map placed in the JSON's root context without the duplicate that contains the property identifier and nesting. I've tried a lot of annotations in various forms; I've tried disabling the ObjectMapper wrap_root_value SerializationFeature; I honestly feel like I've tried just about everything. So before I throw a computer out the window, I'm asking for a second (and beyond) set of eyes to help point out what must be painfully obvious.
Thanks in advance for your attention and advice.
edit: updated the actual output JSON I see now. The data is being duplicated, and I'd like to remove the duplicate that has the nested property.
The problem is that you have 2 accessors exposed for PayloadData: the public property and the getter, so it is being serialized twice. If it is possible, I would recommend restructuring your data class to be immutable. For example:
public class PayloadData {
private final Map<String, String> payloadData;
private final Map<NamespaceEnum, String> namespaces;
#JsonCreator
public PayloadData(#JsonProperty("xmlns") Map<NamespaceEnum, String> namespaces,
#JsonProperty("payloadData") Map<String, String> payloadData) {
this.namespaces = namespaces;
this.payloadData = payloadData;
}
#JsonAnyGetter
public Map<String, String> getPayloadData() {
return payloadData;
}
#JsonProperty("xmlns")
public Map<NamespaceEnum, String> getNamespaces() {
return namespaces;
}
}
This will give you the desired output with out any configuration of the ObjectMapper.
You can parse your json data to a HashMap not a class object:
public HashMap<String, Object> testJackson(String data) throws IOException {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o = mapper.readValue(data, typeRef);
return o
}
Get JSON data from a HashMap:
public String getJsonFromMap(HashMap<String, Object> data) {
return new ObjectMapper().writeValueAsString(data);
}
I have annotated an endpoint with swagger annotations. In the #ResponseHeader I set the returning class as response. This class contains a property which is annotated with #XmlJavaTypeAdapter. The adapter is changing the data type of the property. Unfortunately Swagger shows the type of the property, not the return type of the Adapter. Is it possible to do this?
What I already tried is to annotate the property with #ApiModelProperty(). But it was not possible for me to set the dataType to List (Primitive data types or just a list was working).
Thanks :)
The following was not working:
#ApiModelProperty(dataType = "List<Map<String, String>>")
public Map<String, String> someMap = new HashMap<>();
I had to create an Interface
public interface ListOfMap extends List<Map<String, String>> {}
And then I used this interface in the ApiModelProperty:
#ApiModelProperty(dataType = "ListOfMap")
public Map<String, String> someMap = new HashMap<>();
With this, everything worked :)