gpt4 book ai didi

spring - Spring 如何在没有调试信息的情况下获取参数名称

转载 作者:行者123 更新时间:2023-12-02 10:19:59 25 4
gpt4 key购买 nike

@RequestMapping("/form")
public String form(Model model, Integer id)

例如,spring可以知道参数id的名称是id,并在运行时将请求参数的值绑定(bind)到它

最佳答案

这是 JDK 8 中引入的 javac 功能。您需要包含 -parameters javac 命令行选项来激活它。然后您将能够获取如下参数名称:

String name = String.class.getMethod("substring", int.class).getParameters()[0].getName()
System.out.println(name);

来自 Spring 文档 3.3 Java 8 (as well as 6 and 7)

You can also use Java 8’s parameter name discovery (based on the -parameters compiler flag) as an alternative to compiling your code with debug information enabled.

如 javadoc 中指定的 ParameterNameDiscoverer类:

Parameter name discovery is not always possible, but various strategies are available to try, such as looking for debug information that may have been emitted at compile time, and looking for argname annotation values optionally accompanying AspectJ annotated methods.

这是 Spring 项目中相关 JIRA 项目的链接 Java 8 parameter name discovery for methods and constructors

JDK增强提案JEP 118: Access to Parameter Names at Runtime

关于spring - Spring 如何在没有调试信息的情况下获取参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466913/

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