我想修改一个已经使用JacksonMapper填充的对象,并自动添加IP和Referrer URL,但请求始终为null,因为在attributes数组中找不到它。我做错了吗?
ApiController.java
@RequestMapping(value="/member/follow")
public @ResponseBody IHttpResponse follow(@RequestBody FollowRequest request) {
return request.getHttpResponse();
}
ApiRequestWrapper.js
public class ApiRequestWrapper extends HttpServletRequestWrapper
{
public ApiRequestWrapper(HttpServletRequest request) {
super(request);
if(this.getAttribute("request") instanceof IHttpRequest)
{
IHttpRequest httpRequest = (IHttpRequest) this.getAttribute("request");
if(httpRequest != null)
{
httpRequest.setIp(request.getRemoteAddr());
httpRequest.setReferrer(request.getLocalName());
}
}
}
}
Web.xml中
<filter>
<filter-name>apiFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>apiFilter</filter-name>
<url-pattern>/api/*</url-pattern>
</filter-mapping>
答案 0 :(得分:1)
在调用方法之前创建了与RequestBody对应的对象 - 无法在过滤器中获取它。您可以通过实现Aspect来实现您想要的目标。