spring MVC使用自定义的参数解析器解析参数
生活随笔
收集整理的這篇文章主要介紹了
spring MVC使用自定义的参数解析器解析参数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
寫在前面
編寫自定義的參數(shù)解析器解析請(qǐng)求參數(shù)
項(xiàng)目結(jié)構(gòu)
?定義注解
實(shí)體類
controller
定義參數(shù)解析器
注冊(cè)參數(shù)解析器
啟動(dòng)項(xiàng)目
發(fā)起請(qǐng)求查看結(jié)果
寫在前面
如果還有小伙伴不知道spring MVC的參數(shù)解析原理,什么是參數(shù)解析器的,請(qǐng)移步該博文深入研究:
springboot-springmvc請(qǐng)求參數(shù)獲取與原理【長文預(yù)警,收藏慢啃】_禿了也弱了。的博客-CSDN博客
編寫自定義的參數(shù)解析器解析請(qǐng)求參數(shù)
項(xiàng)目結(jié)構(gòu)
?定義注解
import java.lang.annotation.*;@Documented @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface MyArgs {String value() default ""; }實(shí)體類
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;}@Overridepublic 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;} }定義參數(shù)解析器
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 {/*** 在這里進(jìn)行參數(shù)的類型轉(zhuǎn)換** @param parameter 需要被解析的Controller參數(shù)* @param mavContainer* @param webRequest 當(dāng)前request* @return 轉(zhuǎn)換后的參數(shù)值* @throws Exception*/@Overridepublic 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);// 返回填充好的對(duì)象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);}}/*** 解析器是否支持當(dāng)前參數(shù)** @param methodParameter 需要被解析的Controller參數(shù)* @return*/@Overridepublic boolean supportsParameter(MethodParameter methodParameter) {return methodParameter.hasParameterAnnotation(MyArgs.class);}}注冊(cè)參數(shù)解析器
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 {@Overridepublic void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {// 把自己寫的參數(shù)解析器放在首位,提高命中率if(resolvers.isEmpty()) resolvers.add(0,new RequestParamMethodArgumentResolver());else resolvers.set(0,new RequestParamMethodArgumentResolver());} }啟動(dòng)項(xiàng)目
發(fā)起請(qǐng)求查看結(jié)果
總結(jié)
以上是生活随笔為你收集整理的spring MVC使用自定义的参数解析器解析参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抓住金三银四好机会,超齐全java大厂面
- 下一篇: springBoot-springMVC