<a href="springmvc/testServletAPI">Test ServletAPI</a>
/**
* 可以使用 Serlvet 原生的 API 作为目标方法的参数,具体支持一下类型
* ServletRequest
* ServletResponse
* HttpSession
* Principal
* Locale
* InputStream
* Reader
* OutputStream
* Writer
*/
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request,
HttpServletResponse response){
System.out.println(request+"/n"+response);(断点处)
return SUCCESS;
}
以 Debug 的方式启动 Tomcat .
一、Debug 找到下图位置
下图中的 args 中传入的就是 request, response.
二、进入 resolveHandlerArguments 方法
下图能看到是在处理注解类型的参数。
三、而我们传入的是一般类型的参数,所以在进入下图 resolveCommonArgument 中
四、点击进入下图中 resolveStandardArgument (标准参数)的实现类 AnnotationMethodHandlerAdapter 里
看到下面的方法
@Override
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
if (ServletRequest.class.isAssignableFrom(parameterType) ||
MultipartRequest.class.isAssignableFrom(parameterType)) {
Object nativeRequest = webRequest.getNativeRequest(parameterType);
if (nativeRequest == null) {
throw new IllegalStateException(
"Current request is not of type [" + parameterType.getName() + "]: " + request);
}
return nativeRequest;
}
else if (ServletResponse.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
Object nativeResponse = webRequest.getNativeResponse(parameterType);
if (nativeResponse == null) {
throw new IllegalStateException(
"Current response is not of type [" + parameterType.getName() + "]: " + response);
}
return nativeResponse;
}
else if (HttpSession.class.isAssignableFrom(parameterType)) {
return request.getSession();
}
else if (Principal.class.isAssignableFrom(parameterType)) {
return request.getUserPrincipal();
}
else if (Locale.class == parameterType) {
return RequestContextUtils.getLocale(request);
}
else if (InputStream.class.isAssignableFrom(parameterType)) {
return request.getInputStream();
}
else if (Reader.class.isAssignableFrom(parameterType)) {
return request.getReader();
}
else if (OutputStream.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getOutputStream();
}
else if (Writer.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getWriter();
}
return super.resolveStandardArgument(parameterType, webRequest);
}