如何使@Controller映射路径可配置? [英] How do I make @Controller mapping path configurable?

查看:530
本文介绍了如何使@Controller映射路径可配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个内部库,该库应该自动向Spring MVC应用程序添加一些控制器.这些控制器都是@RestController,带有一些用@RequestMapping注释的处理程序方法.由于它是一个库,因此我希望用户能够指定该库应公开所有这些控制器的根路径.插图:

I'm building an internal library that should automatically add a few controllers to Spring MVC application. These controllers are all @RestController with a few handler methods annotated with @RequestMapping. Since it's a library, I want users to be able to specify the root path where the library should expose all these controllers. Illustration:

// given that I have a controller like this:
@RestController
@RequestMapping("/admin") 
class AdminController {
  @RequestMapping("/users")
  UsersDto allUsers() { ... }
}

// it will be exposed at `http://localhost/admin/users`

我要实现的是使/admin部分可配置.例如,如果用户在application.properties中说:

What I want to achieve is to make /admin part configurable. For example, if user says in application.properties:

super.admin.path=/top/secret/location/here

我希望http://localhost/top/secret/location/here的处理程序在http://localhost/top/secret/location/here可用,因此allUsers()处理程序应具有以下完整路径:

I want AdminController's handlers to be available at http://localhost/top/secret/location/here, and so the allUsers() handler should have a full path of:

http://localhost/top/secret/location/here/users

我该如何实现?感觉这很普通,但是我没有找到一个可行的简单解决方案.

How do I achieve this? Feels like a pretty common task, but I didn't manage to find a straightforward solution that works.

有一种SimpleUrlHandlerMapping机制似乎正是我想要的:

There's a SimpleUrlHandlerMapping mechanism that seems to be exactly what I want:

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
    SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
    simpleUrlHandlerMapping.setOrder(Integer.MAX_VALUE - 2);
    simpleUrlHandlerMapping.setUrlMap(Collections.singletonMap("/ANY_CUSTOM_VALUE_HERE/*", "adminController"));
    return simpleUrlHandlerMapping;
}

但它一直在说

DispatcherServlet配置需要包括一个支持该处理程序的HandlerAdapter

The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

我的发现#2

有一个具有此确切功能的Spring Boot Admin项目-您可以配置它们应公开所有端点的位置.他们似乎在

My finding #2

There's Spring Boot Admin project that has this exact feature - you may configure where they should expose all their endpoints. They seem to have this functionality implemented from scratch in PrefixHandlerMapping. How they use it:

...
@Bean
public PrefixHandlerMapping prefixHandlerMappingNotificationFilterController() {
    PrefixHandlerMapping prefixHandlerMapping = new PrefixHandlerMapping(notificationFilterController());
    prefixHandlerMapping.setPrefix(adminServerProperties.getContextPath());
    return prefixHandlerMapping;
}
...

推荐答案

除了@M.在Deinum解决方案中,您可以将路径模式与占位符一起使用.作为 Spring文档状态:

In addition to @M. Deinum solution, you can use Path Patterns with Placeholders. As Spring documentation states:

批注中的

模式支持${…​}占位符 针对本地属性和/或系统属性和环境 变量.这在控制器路径为的情况下可能很有用 映射到的对象可能需要通过配置进行自定义.

Patterns in @RequestMapping annotations support ${…​} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to be customized through configuration.

因此,在您的情况下,您的控制器将是:

So in your case, your controller would be like:

@RestController
@RequestMapping("/${super.admin.path:admin}") 
class AdminController {
  // Same as before
}

前面的控制器将使用super.admin.path本地/系统属性或环境变量值作为其前缀,或者使用admin(如果未提供).如果您使用的是Spring Boot,请将以下内容添加到application.properties:

The preceding controller would use super.admin.path local/system property or environment variable value as its prefix or admin if those aren't provided. If you're using Spring Boot, by adding the following to your application.properties:

super.admin.path=whatever

您可以自定义该前缀.

这篇关于如何使@Controller映射路径可配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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