Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring如何在没有调试信息的情况下获取参数名_Spring_Spring Mvc_Reflection - Fatal编程技术网

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

Spring如何在没有调试信息的情况下获取参数名,spring,spring-mvc,reflection,Spring,Spring Mvc,Reflection,例如,spring可以知道参数id的名称是id,并在运行时将request param的值绑定到它是WebDataBinder为您执行此操作 正如您在api上看到的,WebDataBinder用于从web请求参数到JavaBean对象的数据绑定 WebDataBinder还负责验证和转换,因此,如果您有自定义编辑器或验证器,则必须将它们添加到控制器中的WebDataBinder中,如下所示 @RequestMapping("/form") public String form(Model mo

例如,spring可以知道参数id的名称是id,并在运行时将request param的值绑定到它

WebDataBinder
为您执行此操作

正如您在api上看到的,WebDataBinder用于从web请求参数到JavaBean对象的数据绑定

WebDataBinder还负责验证和转换,因此,如果您有自定义编辑器或验证器,则必须将它们添加到控制器中的WebDataBinder中,如下所示

@RequestMapping("/form")
public String form(Model model, Integer id)
有关更多信息,您必须查看文档


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

        @Controller
        public class HelloController {

            @InitBinder
            protected void initBinder(WebDataBinder binder) {
                binder.setValidator(yourValidator);
                binder.registerCustomEditor(the clazz to conver to.class, "the name of the parameter", yourEditor);
            }
    ...
来自Spring文档

您还可以使用Java8的参数名发现(基于 -参数(编译器标志),作为在启用调试信息的情况下编译代码的替代方法

如类的javadoc中所述:

参数名称发现并不总是可能的,但会有各种各样的方法 可以尝试一些策略,例如查找调试信息 这可能是在编译时发出的,正在查找argname 注释值(可选)随AspectJ注释方法一起提供

这是到Spring项目中相关JIRA项目的链接


JDK增强方案

默认情况下,Java编译器在类文件中包含调试信息。因此JVM可以在运行时推断参数名<代码>整数id将在运行时作为
id
找到。在没有调试信息的情况下编译代码的奇数情况下,可以将参数名称指定为
@RequestParam(name=“id”)Integer id
String name = String.class.getMethod("substring", int.class).getParameters()[0].getName()
System.out.println(name);