3

Edit: please read the question curefully, I don't need answers that repeat what I wrote.

Looking aroung the web I found quite a confusion about this subject. What I'm looking for is a nice way to extend the value of a Controller's RequestMapping annotation.

Such as:

@Controller
@RequestMapping("/api")
public class ApiController {}

@Controller
@RequestMapping("/dashboard")
public class DashboardApiController extends ApiController {}

The result should be ("/api/dashboard").

This approach apparently simply override the RequestMapping value. A working approach may be to not put a RequestMapping annotation on the derived class.

@Controller
public class DashboardApiController extends ApiController
{
   @GetMapping("/dashboard")
   public String dashboardHome() {
      return "dashboard";
   }

   ... other methods prefixed with "/dashboard"
}

Is this the only feasible approach? I don't really like it.

4

3 Answers 3

3

This is not the elegant solution you're looking for, but here's a functional solution I used.

@Controller
@RequestMapping(BASE_URI)
public class ApiController {
   protected final static String BASE_URI = "/api";
}

@Controller
@RequestMapping(ApiController.BASE_URI + "/dashboard")
public class DashboardApiController extends ApiController {}
Sign up to request clarification or add additional context in comments.

1 Comment

Good solution but I used ApiController.BASE_URI in ApiController annotation not BASE_URI.
0

Values get overridden in the subclasses and not appended. You would need to specify the full path in the child class.

Comments

-1

You can achieve what you are trying to by adding

@Controller
@RequestMapping("/api")
public class DashboardApiController extends WhateverClassWithWhateverMapping
{
   @RequestMapping("/dashboard")
   public String dashboardHome() {
      return "dashboard";
   }

}

In this case it will be "/api/dashboard".

Values for the exact same parameter override on subclasses, they don't accumulate

1 Comment

this doesn't answer the initial question. WhateverClassWithWhateverMapping is useless in your situation but in the question it's the key for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.