最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Controller with two methods with the same path - Stack Overflow

programmeradmin1浏览0评论

I have controller:

@Controller
public class SearchController

in this Controller I have two methods:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, method= {RequestMethod.GET, RequestMethod.POST}, headers = CONTENT_TYPE_JSON)
@Secured(Permissions.VIEW_PRODUCTS)
@ResponseBody
public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
    return search(request);
}

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW)
@Secured(Permissions.VIEW_PRODUCTS)
@ResponseBody
public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return search(request);
}

in this case application is up.

When I hit endpoint without additional parameters it calls the first method and works.

When I hit endpoint with additional parameter e.g date I can see in Developer tools in UI: POST http://localhost:7123/products/search/filters/all/new 400 (Bad Request) and it never reaches any of these two methods.

2nd try - When I add header to second method:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_Products_NEW, method= {RequestMethod.GET, RequestMethod.POST}, headers = CONTENT_TYPE_JSON)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
        return executeSearch(request);
    }

    @RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, headers = "!Content-type=application/json")
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return executeSearch(request);
    }

in this case it's the same as previously - app is up, search without date is working but when i add parameter then no endpoint is hit.

3rd try is to remove method= {RequestMethod.GET, RequestMethod.POST} from first method:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, headers = CONTENT_TYPE_JSON)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
        return executeSearch(request);
    }

    @RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return executeSearch(request);
    }

in this case application is not even getting up with error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'searchController' method 
search.controller.SearchController#searchAllProducts(SearchFiltersRequest)
to { [products/search/filters/all/new]}: There is already 'searchController' bean method
search.controller.SearchController#searchAllProductsJson(SearchFiltersRequest) mapped.

I also noticed that I have a lot of parameters to filter and I can add them, they will be applied and searchAllInstrumentsJson will be hit. When I just add any date e.g creation date then it's not hitting any endpoint anymore.

I tried to debug .js file in developer tools (but I am not fluent in this) and I see that when I search without date then in

f.send(w || a.data == null ? null : a.data);

a.data is "{"area":"AQ","location":"ENG","family":null,status":"Active","startDate":null}"

and when I add date (which is spoiling endpoint being called) a.data is "{"area":"AQ","location":"ENG","family":null,"status":"Active","startDate":"02/03/2025"

so not a big difference but first works and second fails.

How can I fix this controller?

I have controller:

@Controller
public class SearchController

in this Controller I have two methods:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, method= {RequestMethod.GET, RequestMethod.POST}, headers = CONTENT_TYPE_JSON)
@Secured(Permissions.VIEW_PRODUCTS)
@ResponseBody
public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
    return search(request);
}

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW)
@Secured(Permissions.VIEW_PRODUCTS)
@ResponseBody
public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return search(request);
}

in this case application is up.

When I hit endpoint without additional parameters it calls the first method and works.

When I hit endpoint with additional parameter e.g date I can see in Developer tools in UI: POST http://localhost:7123/products/search/filters/all/new 400 (Bad Request) and it never reaches any of these two methods.

2nd try - When I add header to second method:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_Products_NEW, method= {RequestMethod.GET, RequestMethod.POST}, headers = CONTENT_TYPE_JSON)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
        return executeSearch(request);
    }

    @RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, headers = "!Content-type=application/json")
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return executeSearch(request);
    }

in this case it's the same as previously - app is up, search without date is working but when i add parameter then no endpoint is hit.

3rd try is to remove method= {RequestMethod.GET, RequestMethod.POST} from first method:

@RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW, headers = CONTENT_TYPE_JSON)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProductsJson(@RequestBody SearchFiltersRequest request) {
        return executeSearch(request);
    }

    @RequestMapping(value = SEARCH_BY_FILTERS_ALL_PRODUCTS_NEW)
    @Secured(Permissions.VIEW_PRODUCTS)
    @ResponseBody
    public AjaxJsonResponseBuilder searchAllProducts(SearchFiltersRequest request) {
        return executeSearch(request);
    }

in this case application is not even getting up with error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'searchController' method 
search.controller.SearchController#searchAllProducts(SearchFiltersRequest)
to { [products/search/filters/all/new]}: There is already 'searchController' bean method
search.controller.SearchController#searchAllProductsJson(SearchFiltersRequest) mapped.

I also noticed that I have a lot of parameters to filter and I can add them, they will be applied and searchAllInstrumentsJson will be hit. When I just add any date e.g creation date then it's not hitting any endpoint anymore.

I tried to debug .js file in developer tools (but I am not fluent in this) and I see that when I search without date then in

f.send(w || a.data == null ? null : a.data);

a.data is "{"area":"AQ","location":"ENG","family":null,status":"Active","startDate":null}"

and when I add date (which is spoiling endpoint being called) a.data is "{"area":"AQ","location":"ENG","family":null,"status":"Active","startDate":"02/03/2025"

so not a big difference but first works and second fails.

How can I fix this controller?

Share Improve this question edited Feb 6 at 11:52 Michu93 asked Feb 6 at 11:22 Michu93Michu93 5,6977 gold badges56 silver badges93 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Problem was not with controller itself but with SearchFiltersRequest. I got there private Calendar startDate; and for some reason it couldn't be mapped correctly. I had to add:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Calendar launchDate;
发布评论

评论列表(0)

  1. 暂无评论