How to create a Map from YAML configuation in spring boot? - java

I want to create a map of type Map> in spring boot , below is the thing i configured in my application.yml and related java class
labels:
nodetypes:
payment:
- customerId
- emailId
- movileNumber
profile:
loyality:
#Data
#ConfigurationProperties(prefix = "labels")
#Component
public class NodeTypeToResponseProps {
Map<String, List<String>> nodetypes = new HashMap<>();
}
but map is not creating , i am expecting , a map will get created with below data in it
{payment : [customerId,emailId,movileNumber] ,profile:[] ,loyality:[] }
any help on this please ?

Thanks to everyone , who tried to help me in solving the issue , i found the solution for this plugin in my build.gradle
id 'io.freefair.lombok' version '3.8.4'
it is working fine now .

You have to create Arraylists int this YAML configuration which has the name of the attributes. Than your essential able to call your Object by only calling the attribute.
Example:
YamlConfiguration yaml = new YamlConfiguration();
HashMap<String, List<String>> nodetypes = new HashMap<>();
//setter
for(String key :nodetypes.keySet())
yaml.set("path."+key, nodetypes.get(key));
yaml.set("path."+new String("keys"), nodetypes.keySet());
//getter
HashMap<String, List<String>> cp = new HashMap<>();
for(String key:yaml.getStringList("path."+new String("keys")))
cp.put(key, yaml.getStringList("path."+key));

You can trying put #Component before that #ConrigurationProerties

Related

Adding card source works in 19.45.0, fails in 20.0.0 and above

I am playing with Stripe-Java and I'm trying to add a card to a customer.
My code looks like this:
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx");
Map<String, Object> cardParam = new HashMap<String, Object>();
cardParam.put("number", "4242424242424242");
cardParam.put("exp_month", "11");
cardParam.put("exp_year", "2022");
cardParam.put("cvc", "123");
//token
Map<String, Object> tokenParam = new HashMap<String, Object>();
tokenParam.put("card", cardParam);
Token token = Token.create(tokenParam);
//user token
Map<String, Object> sourceParam = new HashMap<String, Object>();
sourceParam.put("source", token.getId());
//add to customer
stripeCustomer.getSources().create(sourceParam);
This works successfully on Stripe-Java version 19.45.0 but not on 20.0.0 or any versions above. Has the method to add a card changed?
A nullpointer exception is thrown
Thanks
This : stripeCustomer.getSources() will be null in v20.0.0 and above of the library because it pins to API version 2020-08-27 where customer.sources was removed by default. [0] [1]
The sources property on Customers is no longer included by default.
You can expand the list but for performance reasons we recommended
against doing so unless needed.
You would need to explicitly expand [2] "sources" when retrieving the Customer in order to populate customer.getSources()
CustomerRetrieveParams params = CustomerRetrieveParams.builder()
.addExpand("sources").build();
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx", params, null);
Also, your code uses the legacy Token API, and is passing raw card details from your server that puts you in PCI scope, you should look into the recommended integration paths : https://stripe.com/docs/payments/accept-a-payment
[0] https://github.com/stripe/stripe-java/blob/master/CHANGELOG.md#2000---2020-08-31
[1] https://stripe.com/docs/upgrades#2020-08-27
[2] https://stripe.com/docs/expand

Spring boot external configuration from windows fileSystem to HashMap

I'm trying to map a configuration file that is located on the Windows FileSystem and populate a Hashmap with that information.
Following Spring documentation and tutorials I have the following:
A Spring bean with the configuration annotations
#Configuration
#ConfigurationProperties(prefix="contact")
#PropertySource("file:///E:/desarrollo/backend/java/proyectos/IW/contact.properties")
public class ContactConfig {
private Map<String, String> groups = new HashMap<>();
public Map<String, String> getContactGroups() {
return this.groups;
}
}
This is my configuration file:
contact.groups.Brasil = brasil
contact.groups.Argentina = argentina
contact.groups.Chile = chile
contact.groups.Spain = espaƱa
contact.groups.Germany = alemania
contact.groups.Colombia = colombia
contact.groups.CostaRica = costa_rica
contact.groups.Ecuador = ecuador
contact.groups.Guatemala = guatemala
contact.groups.Mexico = mexico
contact.groups.Nicaragua = nicaragua
contact.groups.Panama = panama
contact.groups.Peru = peru
contact.groups.ElSalvador = el_salvador
contact.groups.Uruguay = uruguay
contact.groups.IZZIQoE = izzi_mexico
The application compiles but when I debug the Hashmap always come empty, I have tried with different config.properties formats but none of them works.
I fixed it adding a setter method, now the hashmap gets the value from the property file.

About inserting dynamic data into the static content at run time

I have a template like this in properties file: Dear xxxxxx, you are payment is succesfull.
After loading this template from properties file, I want to replace that "xxxxxx" with dynamic data in Java class.
Please help me on this.
I used Message.format("template",object array which is having dynamic data);
Try this way
placeholderReplacementMap is map that contain your static value and dynamic value key pair
Map<String, Object> placeholderReplacementMap = new HashMap<>();
StrSubstitutor substitutor = new StrSubstitutor(placeholderReplacementMap);
placeholderReplacementMap.put("xxxxxx", dynamicValue);
String newString = substitutor.replace("Dear xxxxxx","you are payment is succesful");

Get list of all values that #Value could use in a Spring Boot App

After switching to Spring Boot, I'm having an issue getting the list of properties available for #Value lookup.
I'm using multiple properties files (classpath:application.context, and /config/applicaton.context for overrrides). That means that reading the classpath:application.context file isn't an option because of the overrides.
classpath:application.properties:
foo.bar.someProp=apple
bar.baz.someProp=peaches
baz.foo.someProp=pumpkin
/config/application.properties:
bar.baz.someProp=cheese
Application.java:
public class Application {
#Value("${foo.bar.someProp}")
private String someProp;
#Value("${bar.baz.someProp}")
private String someProp2;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
...
}
There's no way for me to know that the value of bar.baz.someProp is "cheese" or that baz.foo.someProp exists.
I've seen examples using #ConfigurationProperties but I'd have to know the properties in advance, which I don't.
Ideally I'd just like a map of the properties available to #Value lookups that I've added with the properties files.
I know the management console can show the values of individual properties files, but I can't (not allowed to) activate it and I can't figure out how it's getting the list of properties to mimic the behavior.
Thanks!
Update: 2015-09-08
Ideally there would be a method like this:
public Map<String,Object) getEffectiveProperties() {
....
}
Map<String, Object> allProperties = getEffectiveProperties();
for (Map.Entry<String, Integer> entry : allProperties.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
System.out.println("key: " + key + ", value: " + value.toString() );
}
The output would be:
key: foo.bar.someProp, val: apple
key: bar.baz.someProp, val: cheese
key: baz.foo.someProp, val: pumpkin

Rendering a filled form in the template

I'm using Play! framework 20 on a java project and I have a problem with passing a form to the view.
In the controller I have the following code:
Filter filter = new Filter();
//add some state to the filter object
Form<Filter> filterForm = form(Filter.class).fill(filter);
Logger.info("FilterForm: " + filterForm.get().toString()); // So far so good
return ok(filterView.render(filterForm));
And in the template:
#filterForm.hasErrors() // renders false
#filterForm.data().isEmpty() // renders true!!
#* #filterForm.get().toString() *# throws an Exception: No Value
I also get the same error if in the controller I fill the filter state via a Map:
filterForm = filterForm.bind(aMapWithTheState);
This behaviour is only when filling the filter in code. when I do filterForm.bindFromRequest() in other methods all works fine.
Thanks!!
Solved.
I had to use the form's bind method using a map with the state as I did before. But the correct way is to also pass the properties name:
Map<String, String> formState = new HashMap<String, String>();
formState.put("name", name);
formState.put("birthDate", birthDate);
formState.put("address", address);
filterForm = filterForm.bind(formState, "name", "birthDate", "address");
Despite that the documentation says that the property names are not mandatory.

Categories