关于在Interceptor中读取POST方法参数

时间:2018-06-14 10:42:37

标签: java spring-mvc spring-boot java-ee interceptor

我有以下配置控制器,拦截器和服务。

this.dataGridView1.Anchor = 
  ((System.Windows.Forms.AnchorStyles) 
  ((((System.Windows.Forms.AnchorStyles.Top 
  | System.Windows.Forms.AnchorStyles.Bottom) 
  | System.Windows.Forms.AnchorStyles.Left) 
  | System.Windows.Forms.AnchorStyles.Right)));

当我尝试访问POST请求的 package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.example.demo.interceptor.LogInterceptor; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LogInterceptor()); } } package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MineController { @RequestMapping(value = { "/", "/test" }) public String test(Model model) { System.out.println("-------- MainController.login Test --- "); return "test"; } @RequestMapping(value = { "admin/myLogin" }) public String oldLogin(Model model) { System.out.println("-------- MainController.login old login --- "); return "login"; } @RequestMapping(value = { "admin/newLogin" }) public String newLogin(Model model) { System.out.println("-------- MainController.login New login --- "); return "login"; } } package com.example.demo.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class LogInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { long startTime = System.currentTimeMillis(); System.out.println("\n-------- LogInterception.preHandle --- "); System.out.println("Request URL: " + request.getRequestURL()); System.out.println("Request Parameter: " + request.getParameter("name")); System.out.println("Start Time: " + System.currentTimeMillis()); request.setAttribute("startTime", startTime); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("\n-------- LogInterception.postHandle --- "); System.out.println("Request URL: " + request.getRequestURL()); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { long startTime = System.currentTimeMillis(); System.out.println("\n-------- LogInterception.afterCompletion --- "); System.out.println("Request URL: " + request.getRequestURL()); System.out.println("Start Time: " + System.currentTimeMillis()); request.setAttribute("startTime", startTime); } } package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.example.demo.*") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } requestParameter时。它会给我null。

请帮助我如何在Interceptor中获取该参数值。?

1 个答案:

答案 0 :(得分:1)

将您的WebMvcConfig更改为以下内容;

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.example.demo.interceptor.LogInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LogInterceptor());
        registration.addPathPatterns("/**"); // Or any specific path pattern
    }

}