Reputation: 1447
All of my controllers (requestMapping url) start with the same variable.
myController/blablabal
I am thinking about creating a global variable to replace "myController" so in the future if I change the URL name, I only need to change at one place. The way I am currently doing this is to create a bean. My bean config file:
<bean id="controllerURL" class="java.lang.String">
<constructor-arg type="String" value="tt"/>
</bean>
Then in my controller:
@RequestMapping(value ="/${controllerURL}/qrcode/blablbal", method = RequestMethod.GET)
However, it seems that I can not access this variable controllerURL
correctly. Am I missing anything here? Or do we have a better way to create a global variable in Spring MVC?
Upvotes: 0
Views: 22153
Reputation: 198
I would create a BaseController with the base URL
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/base")
public class BaseController {
}
and implement it
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController extends BaseController{
@RequestMapping("/hello")
String hello(){
return "Hello";
}
}
Upvotes: 2
Reputation: 8495
You can create global variable using bean as below.
1.In your bean declaration,
Use java.lang.String
instead of String
in constructor type attribute.
<bean id="controllerURL" class="java.lang.String">
<constructor-arg type="java.lang.String" value="tt"/>
</bean>
In your Controller,
@Autowired String controllerURL;
and
@RequestMapping(value = controllerURL +"/qrcode/blablbal", method = RequestMethod.GET)
Upvotes: 1
Reputation: 159106
Create a constant and use it. The compiler will merge the constant and the remaining url at compile-time.
public class MyConstants {
public static final String PATH_PREFIX = "/myController/blablabal";
}
import MyConstants.PATH_PREFIX;
@Controller
public class MyController {
@RequestMapping(path = PATH_PREFIX + "/qrcode/blablbal", method = RequestMethod.GET)
// method here
}
Upvotes: 1