복붙노트

[SPRING] Java Spring의 오버로드 컨트롤러 메소드

SPRING

Java Spring의 오버로드 컨트롤러 메소드

서로 다른 URL 매개 변수로 다르게 동작해야하는 컨트롤러가 있습니다. 이 같은:

@RequestMapping(method = RequestMethod.GET)
public A getA(@RequestParam int id, @RequestParam String query) {
    ...
}


@RequestMapping(method = RequestMethod.GET)
public A getA(@RequestParam int id) {
    ...
}

하지만이 작동하지 않는 것, 나는 다음과 같은 예외가 :

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map '[controller name]' bean method 

응용 프로그램이 URL 매개 변수에 따라 메서드를 선택하는 방법이 있습니까?

해결법

  1. ==============================

    1.params가 존재해야하는 매핑을 나타냅니다.

    params가 존재해야하는 매핑을 나타냅니다.

    @RequestMapping(method = RequestMethod.GET, params = {"id", "query"})
    public A getA(@RequestParam int id, @RequestParam String query) {
        ...
    }
    
    
    @RequestMapping(method = RequestMethod.GET, params = {"id"})
    public A getA(@RequestParam int id) {
        ...
    }
    
  2. from https://stackoverflow.com/questions/30380498/overload-controller-method-in-java-spring by cc-by-sa and MIT license