I have properties file which has a key company.id like this
company.id=key1=value1,key2=value2,key3=value4
Now, I want to store key-value pair directly in map using #Value of spring
//For example,
//for key in properties file
//company.list=comp1,comp2
#Value("#{'${proposal.provience}'.split(',')}")
List<String> companyList;
the above will directly converts to List.
Please tell me what to write here without using other libraries like Guava
#value(some logic here)
HashMap<String,String> map;
Related
I was able to use java Spring annotation to inject key values pair into the hashmap as follows
validationError.properties file
errorcode.map={\
"default.labOrder.oneservice": "AAAAAA", \
"default.labOrder.patient.firstName": "BBBBB", \
}
I able to use the following code to inject the values into my hashmap as follows
#Value("#{${errorcode.map}}")
private Map<String, String> errorNumberMap;
However if I have a property file with the following values
errorcode.map={\
"LAB": "AAAAAA, BBBB, CCCC", \
"ECP": "AAAAAA, BBBB, CCCC", \
}
and a map Hashmap<AccessType, List> preserveMap = new HashMap() where AccessType is an enum. Does spring has any annotation that will populate my preserveMap value?
Thanks!
Mapping properties directly into bean fields only support simple basic mappings between properties and field values.
Using a configuration object gives more options and can do this kind of tricks and is also more readable.
https://www.baeldung.com/configuration-properties-in-spring-boot
I have application.properties file which contains Map values like below,
myMap={key1:'value1',key2:'value2',....}
Now I know I can read this using,
#Value("#{${myMap}}")
private Map<String,String> myMap;
But I want to read this using Environment API. But I don't see proper method to fetch the Map value as Map. All I see is
import org.springframework.core.env.Environment;
#Autowired
private Environment env;
Map<String,String> myMap = env.getProperty("myMap"); // returns String
How can I get a Map directly from the properties file using the Enviromnet API? Or I need to do conversions on my own?
Any help is appreciated.
I have a "details.yml" file, considering all the setting for getting all values from "yml.file" is done. But I am unable to store Map values into
"Map"
Here is my "details.yml" file below
details:
company:XYZ
values:
name: Manish
last: Raut
And in my class file i am able to get the values of "company" from yml file using #Value("${company}")
#Component
#EnableConfigurationProperties
#ConfigurationProperties(prefix = "details")
public class abcd() {
#Value("${company}")
String company;
#Value("${values}")
Map<String, String> values =new HashMap<String, String>();
...............................
}
i am not able to get those values in Mao which i created in this class, but i am getting values for "Company".
Help me with this?
Im the past, I added getter/setter for MAP type and it was work.
Did you try it? (getter/setter for 'values')
I'm not really sure what marshalling framework Spring uses behind the scenes and how it configures it (you could probably find out with some debugging and maybe make it work for your case), but you could always add an extra layer to your application and configure your own.
For instance you could use Jackson with yaml dataformat - https://dzone.com/articles/read-yaml-in-java-with-jackson.
I am new to solr, and I am facing a problem when I try to serialize/deserialize a Map in Solr.
I use Spring Data Solr in my Java application as follow:
#Field("mapped_*")
private Map<String, String> values;
It flatten and serializes my map in Solr as follow:
"key1" : "value1"
"key2" : "value2"
...
However, when I run a search, the returned objects have this field always set as NULL. Deserialization does not work on this particular field, it looks like it does not recognize the key1, key2... as part of the Map.
Does anyone know how to make the derialization work? Do I have to implement a custom converter?
At this time Spring Data Solr does not automatically prefix values contained in the map with the given #Field#value, but will just use the Map#key as fieldname. There's an improvement (DATASOLR-202) open.
At this time having key1, key2,.. in values requires the fieldname to be key* in order to read back values correctly.
#Field("key*")
private Map<String, String> values;
I have a text file which contain comma separated data which is the attribute of our bean.
e.g. name,age,gender,city,zipcode
We read the text file and we have a list which contain all the attribute. Here we need to create a dynamic Bean which contain the attribute based on that list which we get after reading text file, but we have different text files with different fields. So how should I create a dynamic bean which can contain the attributes according to the list which we will get after reading test file? Please give me some solution on this issue.
It isn't a dynamic Bean,
but I would use a HashMap:
HashMap<String, String> values = new HashMap<String, String>();
values.put("name", "Sebastian Blablabla");
values.put("city", "MyTown");
System.out.println(values.get("name"));
System.out.println(values.containsKey("city"));
System.out.println(values.containsKey("zipcode"));
Dynamic beans by Oracle use Maps too, look here:
http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0210dynamicbeans01.html
I would just Use a Super- Class; Just like, i dont know..
public class Item