具体
我想在 Spring Boot 中做多个 URL 映射(也就是别名)
详细说明
在我的 Spring Boot 应用程序中
Customer Controller 类主要映射到 /customer
URL,如下所示我想创建易于更改的别名
@Controller
@RequestMapping(value = "/customer")
public class CustomerController{
在我在 XML 中进行映射的普通 Spring 应用程序中,我可以进行如下 URL 映射。
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/customer.htm">customerController</prop>
<prop key="/tester.htm">customerController</prop>
</props>
</property>
</bean>
<bean id="customerController"
class="com. ... .controller.CustomerController" />
Spring boot,属性文件配置在大多数情况下都很有用,因为自动配置在屋顶下工作。
- 有什么方法可以使用属性文件来做同样的事情。
- 在 Spring Boot 中进行 URL 映射时应遵循的最佳做法是什么,我可以在编译后轻松更改。
我很累才找到这个。但最后还是得到了 SO 社区的帮助。请帮我解决这个问题。
最佳答案
如果你想从一个 prop 文件中导出映射,那么你可以这样做
在你的application.properties中,添加键值对
url.mapping : /test/sample
在 Controller 上,您可以执行以下操作:
@Controller
@RequestMapping(value = { "${url.mapping}" })
public class CustomerController{
如果您将 url.mapping
作为 jvm arg
提供,而不是在 prop 文件中提供,那么如果更改值,则无需重新编译,只需重新启动(我希望你能做到,我自己还没有尝试过)应该可以解决问题。
对于多个映射,您必须为每个映射添加一个,然后在 Controller 中进行映射,如下所示。
@Controller
@RequestMapping(value = { "${url.mapping}","${url.mapping.two}" })
public class CustomerController{
关于java - 如何在 Spring Boot 中进行多个 URL 映射(别名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634724/