本文共 3265 字,大约阅读时间需要 10 分钟。
自定义解析器需要实现HandlerMethodArgumentResolver
接口,HandlerMethodArgumentResolver
接口包含两个接口函数:
public interface HandlerMethodArgumentResolver { boolean supportsParameter(MethodParameter var1); @Nullable Object resolveArgument(MethodParameter var1, @Nullable ModelAndViewContainer var2, NativeWebRequest var3, @Nullable WebDataBinderFactory var4) throws Exception;}
CurrentUserMethodArgumentResolver
我们在解析器中返回一个固定的UserBeannew UserBean(1L,"admin")
,实际情况是从Session、数据库或者缓存中查。
import org.springframework.core.MethodParameter;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;/** * 用于绑定@CurrentUser的方法参数解析器 * * @author lism */public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver { public CurrentUserMethodArgumentResolver() { } @Override public boolean supportsParameter(MethodParameter parameter) { if (parameter.getParameterType().isAssignableFrom(UserBean.class) && parameter.hasParameterAnnotation(CurrentUser.class)) { return true; } return false; } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { CurrentUser currentUserAnnotation = parameter.getParameterAnnotation(CurrentUser.class); //从Session 获取用户 Object object = webRequest.getAttribute(currentUserAnnotation.value(), NativeWebRequest.SCOPE_SESSION);//从 accessToken获得用户信息 if (object == null) { String token = webRequest.getHeader("Authorization"); if (token == null) { token = webRequest.getParameter("accessToken"); } //为了测试先写死用户名 //TODO: 取真实用户 return new UserBean(1L,"admin"); } return object; }}
@CurrentUser
import java.lang.annotation.*;/** *绑定当前登录的用户
*不同于@ModelAttribute
* * @author lism */@Target({ElementType.PARAMETER})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface CurrentUser { /** * 当前用户在request中的名字 * * @return */ String value() default "user";}
@CurrentUser
在控制器方法上加入@CurrentUser UserBean userBean
即可自动注入userBean的值
@RestController@RequestMapping(value = "/test")public class TestController { /** * 根据name查询 * * @param request * @return */ @RequestMapping(value = "/testCurrentUser", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") @ResponseBody public void test(@CurrentUser UserBean userBean, @RequestBody SubjectRequest request) { String createdBy = userBean.getUsername(); log.info(createdBy); }}
UserBean
import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;@Data@NoArgsConstructor@AllArgsConstructorpublic class UserBean implements Serializable { private Long id; private String username;}
我们可以通过实现HandlerMethodArgumentResolver
接口来实现对自定义的参数进行解析。
转载地址:http://ahfao.baihongyu.com/