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

后端 未结 2 557
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 07:51

In specific

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

In Detail

In my spring boot a

相关标签:
2条回答
  • 2020-12-24 08:07

    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

    Edit:

    For multiple mappings you can use:

    @RequestMapping({ "/home", "/contact" })
    
    0 讨论(0)
  • 2020-12-24 08:09

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

    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{
    

    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{
    
    0 讨论(0)
提交回复
热议问题