Java 如何在 Spring Boot 中进行多 URL 映射(别名)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29634724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do Multiple URL Mapping (aliases) in Spring Boot
提问by Faraj Farook
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 Controllerclass has mapped primarily to the /customer
URL as below I want to create easily changeable aliases
在我的 Spring Boot 应用程序中,
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.
在我在 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, property file configurations are helpful in most of the time as the autoconfig is working under the roof.
Spring boot,属性文件配置在大多数情况下很有帮助,因为自动配置在屋顶下工作。
- Is there any way I can do the same using the property files.
- 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. Please help me on this.
我很累才找到这个。但最终还是得到了 SO 社区的帮助。请帮我解决这个问题。
采纳答案by minion
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.
而不是在 prop 文件中提供,如果你提供url.mapping
as a jvm arg
,那么如果你改变了值,你就不必重新编译,只需重新启动(我希望你能做到,我自己没有尝试过)应该可以解决问题。
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{
回答by Evgeni Dimitrov
Take a look at thisexample.
看看这个例子。
The best way to map url is to do it in the controller with annotations.
映射 url 的最佳方法是在带有注释的控制器中进行。
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/Hello
和localhost:8080/Hello/otherMapping
Edit:
编辑:
For multiple mappings you can use:
对于多个映射,您可以使用:
@RequestMapping({ "/home", "/contact" })