Suppose we have a hierarchy like this:
class Parent {
public static final String BASE_NAME = "parent";
public static final String X_URL = BASE_NAME + "/x";
public static final String Y_URL = BASE_NAME + "/y";
}
class ChildA extends Parent {
public static final String BASE_NAME = "childA";
public static final String X_URL = BASE_NAME + "/x";
public static final String Y_URL = BASE_NAME + "/y";
@RequestMapping(value= X_URL)
public String renderXPage(){...}
}
as you can see the X_URL and Y_URL are repeating both in parent and child, if the child can feed in its own BASE_NAME to the parent, we can eliminate redundant repetition of those constants and then we can use them for the annotation value. How to do this in Java ?
There is no way to reliable achieve this.
You can have @RequestMapping
annotation on class and put base part of URL there.
Example:
@RequestMapping("ChildA")
class ChildA {
@RequestMapping(value= "x")
public String renderXPage(){...}
}
renderXPage
will handle "ChildA/x" URL.
Using the solution of @talex I came up with this neat solution:
public interface CommonRelativeUrls {
String X_URL = "/x";
String Y_URL = "/y";
}
public interface Entities {
String CLASS_A = "class-a";
String CLASS_B = "class-b";
...
}
@RequestMapping(value = Entities.CLASS_A)
public class ClassA implements CommonRelativeUrls {
@RequestMapping(value= X_URL)
public String renderXPage(){...}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.