Home > database >  How to create a list of values inside application.properties file and how to retrieve that list insi
How to create a list of values inside application.properties file and how to retrieve that list insi

Time:06-16

My requirement is to create a list of values inside application.properties file.

com.mail = aaaa, bbbb, cccc 

I want to retrieve these values in my controller class and iterator over each value and should check with the requestbody/queryparam values which gets, when hitting an API

Consider I have an API

@RestController
@RequestMapping("/response")
public class HomeController {
@PostMapping("/postbody")
public String postBody(@RequestBody String fullName) {
   //here I have to validate the fullName with the list I created in the application.properties
Eg: if(fullname.equals(aaaa) or if(fullname.equals(bbbb) or if(fullname.equals(cccc)
// I want to iterator over the list to check any value is matching with fullName.
}}

How to declare list of values inside application.properties? How to retrieve that list inside controller class? Post retrieving how to iterate over the list to check whether it matches with requestbody/queryparam value?

Please provide me with solution. Thank you

CodePudding user response:

Split the list using a comma as the delimiter.

private String[] mailList;

public HomeController( @Value("${com.mail}") final String mail) {
    mailList = mail.split(",")
}

You can now use mailList inside postBody method.

CodePudding user response:

In Application. properties you will add the parameter with values separated with ','

com.mail = aaaa,bbbb,cccc

in the controller  will get the Values 
    @Value("${com.mail}")
    private List<String> mailListValues;
@RestController
@RequestMapping("/response")
public class HomeController {

 @Value("${com.mail}")
    private List<Object> mailListValues;

@PostMapping("/postbody")
public String postBody(@RequestBody String fullName) {
 if(!mailListValues.isEmpty()){
long countOfMatch = mailListValues.stream()
.filter(item->item.equals(fullName)).count();

  if(countOfMatch >0)
// your Business .....
}
}}

CodePudding user response:

use comma separated values in application.properties

com.mail = aaaa, bbbb, cccc

Java code for access

@Value("${com.email}")
String[] mailList;

It worked.

  • Related