매개변수 해석기HandlerMethodArgumentResolver

5379 단어

HandlerMethodArgumentResolver 사용


프로젝트에서 지정한 대상을 신속하게 가져오거나 파라미터를 수정하는 데 사용할 수 있습니다

1. 솔리드 클래스 Person 정의

@Data
public class Person {
    private String firstName;
    private String lastName="notVa";

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

2. Controller를 사용하여 이 Person 가져오기

// firstName lastName
@Controller
public class DemoHandlerMethodArgumentResolverController {

    @RequestMapping(value = "/demoAddPerson")
    @ResponseBody
    public String addPerson(Person person) {
        System.out.println(person.toString());
        return person.toString();
    }
}

3. 매개 변수의 해석 과정을 스스로 정의하고 해석기를 만들 수 있다


들어오는 매개 변수 앞에 new 문자열을 추가합니다. (되돌아오는 대상도 변경할 수 있습니다.)
@Component
// HandlerMethodArgumentResolver 
public class PersonArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().equals(Person.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        return new Person("new"+firstName,"new"+lastName);
    }
}

// 
public interface HandlerMethodArgumentResolver {
    // , true , resolveArgument。
    boolean supportsParameter(MethodParameter parameter);
    // , Object controller 。
    Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
            NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception;

}

4. 이 해상도 등록

// WebMvcConfigurationSupport , 
@Configuration
public class ConfigArgumentResolver extends WebMvcConfigurationSupport {
    @Autowired
    private PersonArgumentResolver personArgumentResolver;

    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(personArgumentResolver);
    }
}

5. 효과 테스트


입력http://localhost:8080/demoAddPerson?firstName=fi&lastName=la반환Person(firstName=newfi, lastName=newla)

다중 Person


1. 메모 추가

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface MultiPerson {
    String value();
}

2. 파서

@Component
public class MultiPersonArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(MultiPerson.class) && methodParameter.getParameterType().equals(Person.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        MultiPerson multiPerson = methodParameter.getParameterAnnotation(MultiPerson.class);
        String firstName = multiPerson.value()+"."+ request.getParameter("firstName");
        String lastName = multiPerson.value()+"."+ request.getParameter("lastName");
        return new Person(firstName,lastName);
    }
}

3. 등록

@Configuration
public class ConfigArgumentResolver extends WebMvcConfigurationSupport {
    @Autowired
    private PersonArgumentResolver personArgumentResolver;

    @Autowired
    private MultiPersonArgumentResolver multiPersonArgumentResolver;

    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        //argumentResolvers.add(personArgumentResolver);
        argumentResolvers.add(multiPersonArgumentResolver);

    }
}

4.Controller

@RequestMapping(value = "/demoAddMultiPerson")
    @ResponseBody
    public String addMultiPerson(@MultiPerson("person1") Person person1,@MultiPerson("person2")Person person2) {
        System.out.println(person1.toString());
        System.out.println(person2.toString());
        return person1.toString()+" --- "+ person2.toString();
    }

5. 효과 테스트


입력http://localhost:8080/demoAddMultiPerson?firstName=fi&lastName=la결과Person(firstName=person1.fi, lastName=person1.la) ---- Person(firstName=person2.fi, lastName=person2.la)

좋은 웹페이지 즐겨찾기