Filosssof
Filosssof

Reputation: 1358

How to solve spring mvc mapping

I have two controllers first one was:

    @Controller("/similarsearch")
public class SimilarSearchController {

  @Autowired
  SimilarSearchService similarSearchService;

  @RequestMapping(method = RequestMethod.GET,produces = "application/json; charset=UTF-8")
  @ResponseBody
  public String loadSimilarSearchPhrases(HttpServletRequest request,
                                         @RequestParam(value = "q", required = true, defaultValue = "") final String query){
    HttpSession session = request.getSession();
    List<List<SimilarLink>> responseMain = (List<List<SimilarLink>>) session.getAttribute("responseMain");
    Map<String, List<String>> result = similarSearchService.getSimilarSearchPhrases(responseMain,query);
    return new JSONSerializer().exclude(JsonHelper.STANDARD_EXCLUDE).include("*").serialize(result);
  }
}

And it worked perfectly, when I accessed localhost:0000/similarsearch/ it returned me needed json. But when I added another controller

@Controller("wiki")
public class WikiController {

  @Autowired
  SearchService searchService;

  @SuppressWarnings("unchecked")
  @RequestMapping(method = RequestMethod.GET,produces = "application/json; charset=UTF-8")
  @ResponseBody
  public String loadWikiInfo(HttpServletRequest request,
                             @RequestParam(value = "q", required = true, defaultValue = "") final String query){
    HttpSession session = request.getSession();
    List<List<SimilarLink>> responseMain = (List<List<SimilarLink>>) session.getAttribute("responseMain");
    WikiInfoLocal wikiInfo = searchService.getWikiInfo(responseMain,query);
    return new JSONSerializer().exclude(JsonHelper.STANDARD_EXCLUDE).include("*").serialize(wikiInfo);
  }
}

I received

java.lang.IllegalStateException: Ambiguous mapping found. Cannot map '/similarsearch' bean method

public java.lang.String org.izsearch.controllers.SimilarSearchController.loadSimilarSearchPhrases(javax.servlet.http.HttpServletRequest,java.lang.String) to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]}: There is already 'wiki' bean method

and I can't use no one of these controller.

Upvotes: 0

Views: 214

Answers (2)

erhun
erhun

Reputation: 3649

Controller defined in Spring Api Documentation as

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

So it will be more robust and suitable to use @RequestMapping to map the request to your controller.

Please use them with this way

SimilarSearchController.java

@Controller
public class SimilarSearchController {

  @Autowired
  SimilarSearchService similarSearchService;

  @RequestMapping(value="/similarsearch",method = RequestMethod.GET,produces = "application/json; charset=UTF-8")
  @ResponseBody
  public String loadSimilarSearchPhrases(HttpServletRequest request,
                                         @RequestParam(value = "q", required = true, defaultValue = "") final String query){

WikiController.java

@Controller
public class WikiController {

  @Autowired
  SearchService searchService;

  @SuppressWarnings("unchecked")
  @RequestMapping(value="/wiki",method = RequestMethod.GET,produces = "application/json; charset=UTF-8")
  @ResponseBody
  public String loadWikiInfo(HttpServletRequest request,
                             @RequestParam(value = "q", required = true, defaultValue = "") final String query){

Basic tutorial about RequestMapping Usage

Detailed tutorial about RequestMapping,RequestParam and PathVariable Usage

Upvotes: 4

csikos.balint
csikos.balint

Reputation: 1105

it should work as you described, however you can see in the logs the registered endpoints. You can see if these controllers are registered or not (or how). You can also try to define endpoint not in the @Controller but at the @RequestMapping annotation with param value see here:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#value--

I hope this will resolve your issue!

Upvotes: 1

Related Questions