目录

​写在前面​

​编写自定义的参数解析器解析请求参数​

​项目结构​

​ 定义注解​

​实体类​

​controller​

​定义参数解析器​

​注册参数解析器​

​启动项目​

​发起请求查看结果​


写在前面

如果还有小伙伴不知道spring MVC的参数解析原理,什么是参数解析器的,请移步该博文深入研究:

​springboot-springmvc请求参数获取与原理【长文预警,收藏慢啃】_秃了也弱了。的博客​

编写自定义的参数解析器解析请求参数

项目结构

spring MVC使用自定义的参数解析器解析参数_spring

 定义注解

import java.lang.annotation.*;

@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyArgs {
String value() default "";
}

实体类

import java.util.Date;

public class Person {

private String id;
private String name;
private Date birthday;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", birthday=" + birthday +
'}';
}
}

controller

import com.cxf.demos.config.MyArgs;
import com.cxf.demos.model.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class ArgumentResolverController {

@RequestMapping("/test")
public String test(@MyArgs Person person) {
System.out.println(person);
return "success value is " + person;
}
}

定义参数解析器

import com.cxf.demos.model.Person;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class RequestParamMethodArgumentResolver implements HandlerMethodArgumentResolver {

/**
* 在这里进行参数的类型转换
*
* @param parameter 需要被解析的Controller参数
* @param mavContainer
* @param webRequest 当前request
* @return 转换后的参数值
* @throws Exception
*/
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
try {
MyArgs annotation = parameter.getParameterAnnotation(MyArgs.class);
String id = webRequest.getParameter("id");
String name = webRequest.getParameter("name");
String birthday = webRequest.getParameter("birthday");
SimpleDateFormat sm = new SimpleDateFormat("yyyyMMdd");
Date parse = sm.parse(birthday);
// 返回填充好的对象
Person person = new Person();
person.setId(id);
person.setName(name);
person.setBirthday(parse);
System.out.println("Person" + person);
return person;
} catch (Exception e) {
throw new IllegalArgumentException("Date format conversion error", e);
}
}

/**
* 解析器是否支持当前参数
*
* @param methodParameter 需要被解析的Controller参数
* @return
*/
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.hasParameterAnnotation(MyArgs.class);
}

}

注册参数解析器

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class ArgumentResolverApplication implements WebMvcConfigurer {

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
// 把自己写的参数解析器放在首位,提高命中率
if(resolvers.isEmpty()) resolvers.add(0,new RequestParamMethodArgumentResolver());
else resolvers.set(0,new RequestParamMethodArgumentResolver());
}
}

启动项目

发起请求查看结果

spring MVC使用自定义的参数解析器解析参数_java_02