Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

@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.

<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. 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.

I tired alot to find this. But at the end ended up in the SO community help. Please help me on this.

like image 545
Faraj Farook Avatar asked Apr 14 '15 18:04

Faraj Farook


People also ask

Can we map multiple URL to a single endpoint?

Note: So, the point that you need to remember is, each resource must have a unique URL, and also it is possible that a resource can be accessed using multiple URLs as long as all the URLs are unique. But it is not possible to access two or more different resources using a single URL in ASP.NET Core Web API Application.

What is difference between @GetMapping and @RequestMapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.

Can two classes have same request mapping?

Unfortunately, this is not possible. The request mapping has to be unique otherwise the application can't determine which method the incoming request should be mapped to.


Video Answer


2 Answers

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{ 
like image 184
minion Avatar answered Sep 20 '22 20:09

minion


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" }) 
like image 34
Evgeni Dimitrov Avatar answered Sep 20 '22 20:09

Evgeni Dimitrov