参数解析器HandlerMethodArgumentResolver
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<HandlerMethodArgumentResolver> 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<HandlerMethodArgumentResolver> 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)