-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestPackage.java
30 lines (24 loc) · 1.24 KB
/
RequestPackage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.zempty.spring_skill_learn.request;
import com.zempty.spring_skill_learn.entity.User;
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;
import javax.servlet.http.HttpServletRequest;
public class RequestPackage implements HandlerMethodArgumentResolver {
// 用来包装 User, 这样 User 就可以在 controller 作为参数使用
@Override
public boolean supportsParameter(MethodParameter parameter) {
System.out.println(parameter);
return User.class.equals(parameter.getParameterType());
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
System.out.println(parameter);
System.out.println(mavContainer);
System.out.println(webRequest);
System.out.println(binderFactory);
return webRequest.getNativeRequest(HttpServletRequest.class).getAttribute("user");
}
}