简体   繁体   English

如何在Spring Boot中执行多个URL映射(别名)

[英]How to do Multiple URL Mapping (aliases) in Spring Boot

In specific 具体而言

I want to do Multiple URL Mapping (in other words aliases) in spring boot 我想在spring boot中进行多URL映射(换句话说别名)

In Detail 详细地

In my spring boot application Customer Controller class has mapped primarily to the /customer URL as below I want to create easily changeable aliases 在我的Spring启动应用程序中, Customer Controller类主要映射到/customer URL,如下所示我想创建易于更改的别名

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

In my normal spring application where I do the mapping in the XML, I can do the URL mapping as below. 在我正常的spring应用程序中,我在XML中进行映射,我可以进行如下的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, property file configurations are helpful in most of the time as the autoconfig is working under the roof. Spring boot,属性文件配置在大多数情况下是有用的,因为autoconfig在屋顶下工作。

  1. Is there any way I can do the same using the property files. 有什么办法可以使用属性文件做同样的事情。
  2. What is the best practice to follow when doing a URL mapping in spring boot which I can change easily after the compilation. 在spring boot中进行URL映射时要遵循的最佳做法是什么,我可以在编译后轻松更改。

I tired alot to find this. 我累了很多才发现这个。 But at the end ended up in the SO community help. 但最终在SO社区的帮助下结束了。 Please help me on this. 请帮帮我。

If you want to drive mapping out of a prop file, then you can do it as below 如果您想要从prop文件中驱动映射,那么您可以执行以下操作

In you application.properties, add the key value pair 在application.properties中,添加键值对

url.mapping : /test/sample

On the controller you can the do the following: 在控制器上,您可以执行以下操作:

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

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. 如果您将url.mapping作为jvm arg提供,而不是提供prop文件,那么如果更改值,则不必重新编译,只需重新启动(我希望您可以这样做,我自己没有尝试过)应该做的伎俩。

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{

Take a look at this example. 看看这个例子。

The best way to map url is to do it in the controller with annotations. 映射网址的最佳方法是在带注释的控制器中执行此操作。

Basically: 基本上:

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

IMHO The best practice is to use one mapping for the controller and one for every method: 恕我直言最佳做法是为控制器使用一个映射,为每个方法使用一个映射:

    @RestController
    @RequestMapping("/Hello")
    public class HelloController {

        @RequestMapping("/")
        public String index() {
            return "Greetings from Spring Boot!";
        }

        @RequestMapping("/otherMapping")
        public String otherMapping() {
            return "Greetings from Spring Boot!";
        }
    }

That way urls will look like: localhost:8080/Hello and localhost:8080/Hello/otherMapping 那样的网址看起来像: localhost:8080/Hellolocalhost:8080/Hello/otherMapping

Edit: 编辑:

For multiple mappings you can use: 对于多个映射,您可以使用:

@RequestMapping({ "/home", "/contact" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM