Tenho um Controller no qual tenho:
@RequestMapping(value = SEARCH_BY_FILTERS_WITH_TYPE, method= {RequestMethod.GET, RequestMethod.POST}, headers = JSON)
@Secured(Permissions.VIEW)
@ResponseBody
public AjaxJsonResponseBuilder searchForFiltersWithTypeJson(@RequestBody SearchFiltersWithTypeRequest request) {
return search(request);
}
@RequestMapping(value = SEARCH_BY_FILTERS_WITH_TYPE, method= {RequestMethod.GET, RequestMethod.POST})
@Secured(Permissions.VIEW)
@ResponseBody
public AjaxJsonResponseBuilder searchForFiltersWithType(SearchFiltersWithTypeRequest request) {
return search(request);
}
e tenho um erro ao iniciar o aplicativo:
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#searchForFiltersWithTypeRequest(SearchFiltersWithType)
to {[GET, POST] [/search/filters/type]}: There is already 'searchController' bean method
search.controller.SearchController#searchForFiltersWithTypeRequest(SearchFiltersWithTypeRequest) mapped.
Versão de primavera:<spring.version>5.3.34</spring.version>
Sim, você pode ter dois ou mais métodos @RequestMapping idênticos com cabeçalhos diferentes. No entanto, você precisa especificar explicitamente cabeçalhos diferentes em cada método. No seu caso, você especificou um cabeçalho apenas para o
searchForFiltersWithTypeJson
método, mas não especificou um cabeçalho para osearchForFiltersWithType
método. Portanto, o Spring não pode determinar qual método deve manipular solicitações com um cabeçalho JSON. Especifique um cabeçalho para o segundo método que seja diferente do primeiro para se livrar do erro.