Ambiguous mapping found when I use extends clause

2019-08-04 20:02发布

问题:

I have a CustomerController and CustomerCardController classes that extend CrudMethods class.

I choose so, because these two controller use the same methods (getFileCustomer() is one of these). And I thought that it had been better use the inheritance to save code's rows.

CustomerController

@Controller
public class CustomerController extends CrudMethods {
...

CustomerCardController

@Controller
public class CustomerCardController extends CrudMethods{
...

CrudMethos

@Controller
public abstract class CrudMethods {

@RequestMapping("getFileCustomer")
@ResponseBody
public String getFileCustomer() {
...

When I run, GlassFish gives me that error:

HTTP Status 500 - Internal Server Error type Exception report

message: Internal Server Error

description: The server encountered an internal error that prevented it from fulfilling this request.

exception:

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception

root cause: Ambiguous mapping found

The problems are @RequestMapping and @ResponseBody annotations.

In fact, if I remove them the application starts.

But I need of these annotation. Because, the @RequestMapping and @ResponseBody are used by Ajax call when the application runs.

And getFileCustomer() is used in both of controllers.

How can solve it?

Thanks

回答1:

@RequestMapping("getFileCustomer") this request mapping is present in both CustomerCardController and CustomerController class since they extend same class which has this mapping. So you have duplicate entry for same request mapping.