Spring boot 能否根据属性文件的内容动态创建端点? [英] Can Spring boot dynamically create end points based on the content of the property file?

查看:56
本文介绍了Spring boot 能否根据属性文件的内容动态创建端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是我想在服务器启动时访问 application.properties,读取这样存储的数据:

But I would like to reach the application.properties when the server starts, to read out the data stored like this:

methods: {
"endpointOne": "DBStoredProcedure1",
"endpointTwo": "DBStoredProcedure2"
}

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

So when my Spring Boot application starts, it would create all the POST endpoints based on the property file with the names of the first parameters (like "endpointOne"), and would call (and return the result of) the stored procedure which is defined as the second parameter (like "DBStoredProcedure1").

可以吗?

推荐答案

是的,你可以.与您目前尝试这样做的方式略有不同.

Yes you can. A little bit differently though than you try to do it at the moment.

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

The best is to use a "PathVariable" - you find detailed information here:

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

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

您在 Controller 类中的方法如下所示:

Your method at the Controller class would look something like this:

 @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; 
 }

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

Obviously you need to implement a method to handle those queries which are not found in the property file, but you get the idea.

这篇关于Spring boot 能否根据属性文件的内容动态创建端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆