Spring可以根据属性文件的内容动态创建端点吗?

Mac*_*rik 4 java rest spring

到目前为止,我正在创建这样的终点:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody String indexPost(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
//Doing calculations
return "Result";
}
Run Code Online (Sandbox Code Playgroud)

但我想在服务器启动时到达application.properties,读出存储的数据,如下所示:

methods: {
"endpointOne": "DBStoredProcedure1",
"endpointTwo": "DBStoredProcedure2"
}
Run Code Online (Sandbox Code Playgroud)

因此,当我的Spring Boot应用程序启动时,它将基于属性文件创建所有POST端点,其中包含第一个参数的名称(如"endpointOne"),并将调用(并返回结果)定义的存储过程作为第二个参数(如"DBStoredProcedure1").

有可能吗?

Mr.*_*man 8

是的你可以.虽然比你现在尝试做的有点不同.

最好使用"PathVariable" - 您可以在此处找到详细信息:

https://spring.io/guides/tutorials/bookmarks/

http://javabeat.net/spring-mvc-requestparam-pathvariable/

您在Controller类中的方法看起来像这样:

 @RequestMapping(value="/{endPoint}", method=RequestMethod.POST)
public String endPoint(@PathVariable String endPoint) {
   //Your endPoint is now the one what the user would like to reach
   //So you check if your property file contains this String - better to cache it's content
   //If it does, you call the service with the regarding Stored Procedure.
   String sPName = prop.getSPName(endPoint); //You need to implement it.
   String answer = yourService.execute(sPName);
   return answer; 
 }
Run Code Online (Sandbox Code Playgroud)

显然,你需要实现一个方法来处理那些在属性文件中找不到的查询,但是你明白了.