gpt4 book ai didi

java - 将自定义参数添加到 spring mvc Controller

转载 作者:行者123 更新时间:2023-12-01 12:59:47 26 4
gpt4 key购买 nike

我有一个自定义调度程序 servlet,它扩展了默认的 DispatcherServlet 来对所有请求进行一些验证。

由于我将从请求(getInputStream()->Map)获取所有参数来进行一些验证,因此我想将参数传递给 Controller ​​或将参数添加到上下文,以便我可以从 Controller 再次获取它们.

现在我只是将所有参数放入全局 Map,但我想知道是否有一些简单的方法。

public class CustomDispatcherServlet extends DispatcherServlet {

private static final long serialVersionUID = 7250693017796274410L;

@Override
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

doFilter(request, response);
super.doDispatch(request, response);
}
...

private void doFilter(HttpServletRequest request, HttpServletResponse response) {
WLNResponse<String> error = null;

try {
boolean isSignValid = checkSignValidity(request);
...

private boolean checkSignValidity(HttpServletRequest request) throws IOException {
// pass this params to controller or somewhere I can get from controller
Map<String, Object> params = WebUtils.readParams(request);
...

最佳答案

我在 Controller 本身中验证参数的方式。例如

@Controller
public ControllerClass
{
@RequestMapping(value = "/someurl", method = RequestMethod.GET, params = {
"requestParam"})
public void someMethod(@RequestParam(value = "requestParam") String requestParam)
{
System.out.println("This is the value of the RequestParam requestParam " + requestParam);
}
}

这样您就可以在 Controller 中进行验证。

唯一无法解决的问题是发出的请求是否未解析到有效的 Controller 。为此,我将使用注释@controllerAdvice

关于java - 将自定义参数添加到 spring mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595251/

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