到目前为止,我正在创建这样的终点:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody String indexPost(HttpServletRequest request, HttpServletResponse response)
throws Exception {
//Doing calculations
return "Result";
}
但我想在服务器启动时到达application.properties,读出这样存储的数据:
methods: {
"endpointOne": "DBStoredProcedure1",
"endpointTwo": "DBStoredProcedure2"
}
所以当我的Spring Boot应用程序启动时,它将根据属性文件创建所有POST端点,其中包含第一个参数的名称(如" endpointOne"),以及将调用(并返回结果)存储过程定义为第二个参数(如" DBStoredProcedure1")。
有可能吗?
答案 0 :(得分:7)
是的,你可以。虽然目前尝试这样做有点不同。
最好使用“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;
}
显然你需要实现一个方法来处理那些在属性文件中找不到的查询,但是你明白了。
答案 1 :(得分:0)
您可以在控制器中使用通配符“ / *”作为值。这样您的所有端点都可以使用相同的控制器请求方法。
下面是代码示例。
@RequestMapping(value = "/*", method = RequestMethod.GET, headers="Accept=*/*", produces = { "application/json" })
public ResponseEntity<Object> getData(@RequestParam Map<String, String> reqParam, HttpServletRequest request)
throws WriteException, IOException{
MessageLogger.infoLog(EquityControllerImpl.class, GETQADTRSTOCKPRICELOGS,
ENTRY);
// Get Request URI
MessageLogger.infoLog("Request URI: " + request.getRequestURI());
String requestUri = request.getRequestURI();
//Read all request parameters
Map<String, String> requestParamMap = new HashMap<>();
for (Map.Entry<String, String> param: reqParam.entrySet()
) {
System.out.println("Parameter: " + param.getKey() + " ----> Value: " + param.getValue());
requestParamMap.put(param.getKey(),param.getValue());
}
}
您还可以定义静态swagger.json并在swagger配置中使用它。
@Configuration
@EnableSwagger2
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.finance.dataplatform.*"))
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build().apiInfo(getApiInfo());
}
private ApiInfo getApiInfo() {
return new ApiInfoBuilder().title("Investment Management").build();
}
private static Predicate<String> matchPathRegex(final String... pathRegexs) {
return new Predicate<String>() {
@Override
public boolean apply(String input) {
for (String pathRegex : pathRegexs) {
if (input.matches(pathRegex)) {
return true;
}
}
return false;
}
};
}
@Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/config/swagger.json")
.addResourceLocations("classpath:/config");
}
};
}
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider(InMemorySwaggerResourcesProvider defaultResourcesProvider) {
return () -> {
SwaggerResource wsResource = new SwaggerResource();
wsResource.setName("default");
wsResource.setSwaggerVersion("2.0");
wsResource.setLocation("/config/swagger.json");
//List<SwaggerResource> resources = new ArrayList<>(defaultResourcesProvider.get());
List<SwaggerResource> resources = new ArrayList<>();
resources.add(wsResource);
return resources;
};
}
}