本文介绍了如何检查 spring RestController 的未知查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受参数的基本休息控制器.

I have a basic rest controller taking parameters.

如果查询字符串包含我没有定义的参数,我如何拒绝连接?

How can I refuse the connection if the query string contains parameters that I did not define?

@RestController
@RequestMapping("/")
public class MyRest {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String content(@PathVariable id, @RequestParam(value = "page", required = false) int page) {
        return id;
    }
}

localhost:8080/myapp/123?pagggge=1

目前调用这个url时,方法只执行id,忽略了未知的paggge参数.一般来说,这很好,但我如何验证它们并返回 HTTP 状态代码?

Currently when calling this url, the method is executed with just the id, and the unknown paggge parameter is just neglected. Which is fine in general, but how can I validate them and in case return a HTTP status code?

推荐答案

在方法参数中添加HttpServletRequest请求,做

String query = request.getQueryString()

在方法体中并验证它.

这篇关于如何检查 spring RestController 的未知查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:05