SpringMVC杂记(十七) HandlerMethodArgumentResolver接口应用example

自从spring3.1 开始就有了这个接口,可以为@RequestMapping标注的方法扩展传入的参数。 
以shiro为例,扩展一个标注,@CurrentUser,只要有这个标注,就可以在shiro的安全上下文中取出适当的对象直接从参数传入,request响应函数。 

Java代码   收藏代码
  1. import java.lang.annotation.Documented;  
  2. import java.lang.annotation.ElementType;  
  3. import java.lang.annotation.Retention;  
  4. import java.lang.annotation.RetentionPolicy;  
  5. import java.lang.annotation.Target;  
  6.   
  7. @Documented  
  8. @Target({ElementType.PARAMETER})  
  9. @Retention(RetentionPolicy.RUNTIME)  
  10. public @interface CurrentUser {  
  11. }  

Java代码   收藏代码
  1. @RequestMapping(value = "/test1")  
  2. public @ResponseBody String test1(@CurrentUser Long userId) {  
  3.     return userId.toString();  
  4. }  
  5.   
  6. @RequestMapping(value = "/test2")  
  7. public @ResponseBody String test2(@CurrentUser UserDetails userDetails) {  
  8.     return userDetails.toString();  
  9. }  

Java代码   收藏代码
  1. import java.lang.annotation.Annotation;  
  2.   
  3. import org.springframework.core.MethodParameter;  
  4. import org.springframework.web.bind.support.WebDataBinderFactory;  
  5. import org.springframework.web.context.request.NativeWebRequest;  
  6. import org.springframework.web.method.support.HandlerMethodArgumentResolver;  
  7. import org.springframework.web.method.support.ModelAndViewContainer;  
  8.   
  9. import com.github.yingzhuo.mycar2.annotation.CurrentUser;  
  10. import com.github.yingzhuo.mycar2.security.SecurityUtils;  
  11. import com.github.yingzhuo.mycar2.security.UserDetails;  
  12.   
  13. public class CurrentUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {  
  14.   
  15.     @Override  
  16.     public boolean supportsParameter(MethodParameter parameter) {  
  17.         Class<?> klass = parameter.getParameterType();  
  18.         if (klass.isAssignableFrom(UserDetails.class) || klass.isAssignableFrom(Long.class)) {  
  19.             Annotation[] as = parameter.getParameterAnnotations();  
  20.             for (Annotation a : as) {  
  21.                 if (a.annotationType() == CurrentUser.class) {  
  22.                     return true;  
  23.                 }  
  24.             }  
  25.         }  
  26.         return false;  
  27.     }  
  28.   
  29.     @Override  
  30.     public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,  
  31.             WebDataBinderFactory binderFactory) throws Exception {  
  32.           
  33.         if ((SecurityUtils.isAuthenticated() || SecurityUtils.isRemembered()) == false) {  
  34.             return null;  
  35.         }  
  36.           
  37.         Class<?> klass = parameter.getParameterType();  
  38.           
  39.         UserDetails userDetails = SecurityUtils.getUserDetails();  
  40.           
  41.         if (klass.isAssignableFrom(UserDetails.class)) {  
  42.             return SecurityUtils.getUserDetails();  
  43.         }  
  44.           
  45.         if (klass.isAssignableFrom(Long.class)) {  
  46.             return userDetails != null ? userDetails.getId() : null;  
  47.         }  
  48.           
  49.         return null;  
  50.     }  
  51. }  

最后,需要配置一下 
Xml代码   收藏代码
  1. <mvc:annotation-driven>  
  2.     <mvc:argument-resolvers>  
  3.         <bean class="xxx.yyy.CurrentUserHandlerMethodArgumentResolver" />  
  4.     </mvc:argument-resolvers>  
  5. </mvc:annotation-driven>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC提供了三种类型转换的接口,分别是Formatter、Converter和原生类型转换器。 1. Formatter接口:Formatter接口用于将字符串转换为目标类型,并将目标类型转换为字符串。它可以用于处理日期、数字等常见的数据类型转换。Formatter接口定义了两个方法:`parse()`用于将字符串转换为目标类型,`print()`用于将目标类型转换为字符串。 2. Converter接口:Converter接口用于将一种数据类型转换为另一种数据类型。它定义了两个方法:`convert()`用于将源类型转换为目标类型,`reverse()`用于将目标类型转换回源类型。开发者可以根据自己的需求实现Converter接口来实现特定功能的类型转换。 3. 原生类型转换器:SpringMVC内置了许多原生类型转换器,可以将常见的数据类型进行转换,例如将字符串转换为整型、将字符串转换为日期等。开发者在实际应用中使用框架内置的类型转换器基本上就够了,但有时需要编写具有特定功能的类型转换器。 下面是一个使用Formatter接口的示例: ```java import org.springframework.format.Formatter; public class MyDateFormatter implements Formatter<Date> { @Override public Date parse(String text, Locale locale) throws ParseException { // 将字符串转换为日期类型 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.parse(text); } @Override public String print(Date date, Locale locale) { // 将日期类型转换为字符串 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(date); } } ``` 下面是一个使用Converter接口的示例: ```java import org.springframework.core.convert.converter.Converter; public class MyStringToIntegerConverter implements Converter<String, Integer> { @Override public Integer convert(String source) { // 将字符串转换为整型 return Integer.parseInt(source); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值