日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時(shí)間:2025/3/19 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。