복붙노트

[SPRING] Errors / BindingResult 인수는 모델 특성, @RequestBody 또는 @RequestPart 인수 바로 다음에 선언되어야합니다.

SPRING

Errors / BindingResult 인수는 모델 특성, @RequestBody 또는 @RequestPart 인수 바로 다음에 선언되어야합니다.

나는 예제 애플리케이션을 해부하고 해부 도중 개발 한 이론을 테스트하기 위해 여기저기서 코드를 추가하여 스프링을 가르치고있다. Spring 애플리케이션에 추가 한 코드를 테스트 할 때 다음 오류 메시지가 나타납니다.

An Errors/BindingResult argument is expected to be declared immediately after the  
model attribute, the @RequestBody or the @RequestPart arguments to which they apply  

오류 메시지가 참조하는 방법은 다음과 같습니다.

@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(Integer typeID, BindingResult result, Map<String, Object> model) {
    // find owners of a specific type of pet
    typeID = 1;//this is just a placeholder
    Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
    model.put("selections", results);
    return "owners/catowners";
}  

이 오류 메시지는 웹 브라우저에서 / catowners url 패턴을로드하려고 할 때 실행되었습니다. 이 페이지와이 게시물을 검토했지만 설명이 명확하지 않은 것 같습니다.

아무도 나에게이 오류를 수정하는 방법을 보여줄 수 있고 또한 그것이 무엇을 의미하는지 설명 할 수 있습니까?

편집하다: Biju Kunjummen의 응답을 토대로 구문을 다음과 같이 변경했습니다.

@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid Integer typeID, BindingResult result, Map<String, Object> model)  

여전히 동일한 오류 메시지가 나타납니다. 내가 이해하지 못하는 것이 있습니까?

두 번째 편집 :

Sotirios의 의견을 바탕으로이 코드를 다음과 같이 변경했습니다.

@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(BindingResult result, Map<String, Object> model) {
    // find owners of a specific type of pet
    Integer typeID = 1;//this is just a placeholder
    Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
    model.put("selections", results);
    return "owners/catowners";
 }

이클립스가 서버에서 다시 실행되도록 지시 한 후에도 여전히 동일한 오류 메시지가 표시됩니다.

내가 이해하지 못하는 것이 있습니까?

해결법

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

    1.Spring은 HandlerMethodArgumentResolver라는 인터페이스를 사용하여 핸들러 메소드에서 매개 변수를 분석하고 인수로 전달할 객체를 생성합니다.

    Spring은 HandlerMethodArgumentResolver라는 인터페이스를 사용하여 핸들러 메소드에서 매개 변수를 분석하고 인수로 전달할 객체를 생성합니다.

    그것이 하나를 찾지 못한다면, null을 넘깁니다 (나는 이것을 검증해야합니다).

    BindingResult는 @ModelAttribute, @Valid, @RequestBody 또는 @RequestPart의 유효성을 검사 할 수있는 오류가있는 결과 개체이므로 주석으로 표시된 매개 변수에만 사용할 수 있습니다. 각 주석에 대해 HandlerMethodArgumentResolver가 있습니다.

    편집 (의견에 대한 답변)

    귀하의 예제는 사용자가 애완 동물 유형 (정수로)을 제공해야한다는 것을 보여줍니다. 나는 그 방법을 다음과 같이 바꿀 것이다.

    @RequestMapping(value = "/catowners", method = RequestMethod.GET)
    public String findOwnersOfPetType(@RequestParam("type") Integer typeID, Map<String, Object> model)
    

    그리고 (설정에 따라) 요청을

    localhost:8080/yourcontext/catowners?type=1
    

    여기에는 유효성을 검사 할 것이 없으므로 BindingResult가 필요하거나 필요하지 않습니다. 어쨌든 추가하려고하면 실패 할 것입니다.

  2. ==============================

    2.BindingResult 유형의 매개 변수가있는 경우 기본적으로 HTTP 요청 매개 변수를 BindingResult 메서드 매개 변수 바로 앞에 선언 된 변수에 바인딩 할 때 오류가 발생합니다.

    BindingResult 유형의 매개 변수가있는 경우 기본적으로 HTTP 요청 매개 변수를 BindingResult 메서드 매개 변수 바로 앞에 선언 된 변수에 바인딩 할 때 오류가 발생합니다.

    그래서 이것들은 모두 받아 들일 수 있습니다 :

    @RequestMapping(value = "/catowners", method = RequestMethod.GET)
    public String findOwnersOfPetType(@Valid MyType type, BindingResult result, ...)
    
    
    @RequestMapping(value = "/catowners", method = RequestMethod.GET)
    public String findOwnersOfPetType(@ModelAttribute MyType type, BindingResult result, ...)
    
    @RequestMapping(value = "/catowners", method = RequestMethod.GET)
    public String findOwnersOfPetType(@RequestBody @Valid MyType type, BindingResult result, ...)
    
  3. from https://stackoverflow.com/questions/18646121/an-errors-bindingresult-argument-is-expected-to-be-declared-immediately-after-th by cc-by-sa and MIT license