本文介绍了如何在Spring Boot中进行多个URL映射(别名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体

我想在春季启动时进行多个URL映射(换句话说,就是别名)

I want to do Multiple URL Mapping (in other words aliases) in spring boot

详细信息

在我的Spring Boot应用程序中客户控制器类主要映射到/customer URL,如下所示,我想创建易于更改的别名

In my spring boot applicationCustomer Controller class has mapped primarily to the /customer URL as below I want to create easily changeable aliases

@Controller
@RequestMapping(value = "/customer")
public class CustomerController{

在普通的Spring应用程序中,我以XML进行映射,因此可以按以下方式进行URL映射.

In my normal spring application where I do the mapping in the XML, I can do the URL mapping as below.

<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, property file configurations are helpful in most of the time as the autoconfig is working under the roof.

  1. 有什么办法可以使用属性文件来做同样的事情.
  2. 在Spring Boot中进行URL映射时应遵循的最佳实践是什么,在编译后我可以轻松地对其进行更改.

我很累才找到这个.但是最后还是在SO社区中获得了帮助.请帮助我.

推荐答案

如果您想将驱动文件映射出prop文件,则可以按照以下步骤进行操作

If you want to drive mapping out of a prop file, then you can do it as below

在application.properties中,添加键值对

In you application.properties, add the key value pair

url.mapping : /test/sample

在控制器上,您可以执行以下操作:

On the controller you can the do the following:

@Controller
@RequestMapping(value = { "${url.mapping}" })
public class CustomerController{

如果您将 url.mapping 作为 jvm arg 提供,而不是在prop文件中提供,那么如果您更改了值,则无需重新编译,只需重新启动(我希望您可以做,但我自己还没有尝试过)就可以解决问题.

Instead of providing in prop file, if you provide the url.mapping as a jvm arg, then you don't have to recompile if you change the value, just restart (which i hope you can do, have not tried it myself) should do the trick.

对于多个映射,您将必须为每个映射添加一个,然后将其映射到如下所示的控制器中.

For multiple mappings, you will have to add one per mapping and map that in controller like below.

@Controller
@RequestMapping(value = { "${url.mapping}","${url.mapping.two}" })
public class CustomerController{

这篇关于如何在Spring Boot中进行多个URL映射(别名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:50