spring MVC使用自定义的参数解析器解析参数
生活随笔
收集整理的這篇文章主要介紹了
spring MVC使用自定义的参数解析器解析参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
寫在前面
編寫自定義的參數解析器解析請求參數
項目結構
?定義注解
實體類
controller
定義參數解析器
注冊參數解析器
啟動項目
發起請求查看結果
寫在前面
如果還有小伙伴不知道spring MVC的參數解析原理,什么是參數解析器的,請移步該博文深入研究:
springboot-springmvc請求參數獲取與原理【長文預警,收藏慢啃】_禿了也弱了。的博客-CSDN博客
編寫自定義的參數解析器解析請求參數
項目結構
?定義注解
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;}@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;} }定義參數解析器
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*/@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);// 返回填充好的對象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*/@Overridepublic 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 {@Overridepublic void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {// 把自己寫的參數解析器放在首位,提高命中率if(resolvers.isEmpty()) resolvers.add(0,new RequestParamMethodArgumentResolver());else resolvers.set(0,new RequestParamMethodArgumentResolver());} }啟動項目
發起請求查看結果
總結
以上是生活随笔為你收集整理的spring MVC使用自定义的参数解析器解析参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抓住金三银四好机会,超齐全java大厂面
- 下一篇: java加载一个来自项目之外的java文