我要創建一個webservice的休息申請,我需要加載存在作爲參數傳遞的文件夾中的所有文件JSON(在application.yml先驗) ,在應用程序啓動時,稍後在webservices的方法中使用它們作爲bean的列表(每個JSON文件對應一個bean)。最佳方式
的樣品來進一步說明我的要求:
application.yml:
json.config.folder: /opt/my_application/json_configs
MyApplication.java:具有這種結構
package com.company;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
JSON文件:
{
"key":"YYYYY",
"operator_list":[
{
"name":"operator1",
"configs":{
"id":"XXXXX1",
"path":"xxxx2"
}
},
{
"name":"operator2",
"configs":{
"id":"XXXXX1",
"passphrase":"xxxx2",
"user_id":"XXXX3",
"password":"XXXXX"
}
},
{
"name":"operator3",
"configs":{
"user_id":"XXXXX1"
}
}
]
}
RestAPI.java
@RestController
@RequestMapping("/my_app_url")
@PropertySource(value={"classpath:application.yml"})
public class RestAPI {
//Some fields
....
//Some methods
....
//Method that return operator list of a given context (correspond to the field "key" of the json file)
@RequestMapping("/getOperatorList")
public List<Operator> getOperatorList(@RequestParam(value = "context", defaultValue = "YYYYY") String context) throws Exception{
List<Operator> result = null;
//Here, i need to loop the objects , that are supposed to be initialized during application startup
//(but i I do not know yet how to do it) with data from JSON files
//to find the one that correspond to the context in parameter and return its operator list
return result;
}
}
ContextOperatorBean.java將包含JSON文件的相關信息先驗:
package com.company.models;
import java.util.List;
public class ContextOperatorBean {
String key;
List<Operator> operator_list;
public ContextOperatorBean() {
}
public ContextOperatorBean(String key, List<PaymentMethod> operator_list) {
this.key = key;
this.operator_list = operator_list;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<Operator> getOperator_list() {
return operator_list;
}
public void setOperator_list(List<Operator> operator_list) {
this.operator_list = operator_list;
}
}
並號召Operator.java包含所有運營商的相關信息的類。
是否有一種方法可以在應用程序啓動時初始化一個包含所有JSON文件信息的ContextOperatorBean
對象列表,並在我的webservice方法(RestAPI.java類)中使用它們?
_「(...)在Web服務的方法,如豆類列表中使用它們(每JSON文件對應於一個bean)」 _ - 你能更詳細地解釋這一點,最好展示一個例子。 – kryger