gpt4 book ai didi

java - 关于Interceptor中读取POST方法参数

转载 作者:行者123 更新时间:2023-12-02 11:07:32 24 4
gpt4 key购买 nike

我对 Controller 、拦截器和服务有以下配置。

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);
}
}

当我尝试访问 POST 请求的 name requestParameter 时,它给了我 null。

请帮助我如何在拦截器中获取该参数值?

最佳答案

将您的 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
}

}

关于java - 关于Interceptor中读取POST方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50855591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com