为什么@RequestMapping注解在java中接受String参数,而在scala中不接受? [英] Why does @RequestMapping annotation accept String parameter in java but not in scala?

查看:44
本文介绍了为什么@RequestMapping注解在java中接受String参数,而在scala中不接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读@RequestMapping 文档:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html

Reading the @RequestMapping documentation : http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html

它接受一个字符串数组参数作为其路径映射.

It accepts a String array parameter for its path mapping.

所以这可以使用 java :

So this works using java :

@RequestMapping("MYVIEW")

但在 Scala 中我需要使用:

but in scala I need to use :

@RequestMapping(Array("MYVIEW"))

Scala 版本是有意义的,因为注释需要一个字符串数组.但是为什么上面在java中工作,它应该不会给出编译时错误吗?

The scala version makes sense as the annotation expects a String array. But why does above work in java, should it not give a compile time error ?

在'ArrayChecker'类(我写的一个类来说明这一点)下面导致java编译时错误:

Below class 'ArrayChecker' (a class I wrote to illustrate this point) causes a java compile time error :

ArrayChecker 类型中的方法 acceptArrayParam(String[]) 不是适用于参数(字符串)

The method acceptArrayParam(String[]) in the type ArrayChecker is not applicable for the arguments (String)

public class ArrayChecker {

    public static void main(String args[]){

        String[] strArray;

        acceptArrayParam("test");
    }

    private static void acceptArrayParam(String[] param){

    }
}

类似的错误应该不是由@RequestMapping("MYVIEW") 引起的吗?

Should a similar error not be caused by @RequestMapping("MYVIEW") ?

推荐答案

Section 9.7.1 Java SE 规范声明:

Section 9.7.1 of the Java SE specification states:

如果元素类型是数组类型并且对应的 ElementValue 不是 ElementValueArrayInitializer,那么唯一元素是 ElementValue 表示的值的数组值与该元素相关联.否则,如果对应的 ElementValue 是 ElementValueArrayInitializer,则 ElementValueArrayInitializer 表示的数组值与该元素相关联.

If the element type is an array type and the corresponding ElementValue is not an ElementValueArrayInitializer, then an array value whose sole element is the value represented by the ElementValue is associated with the element. Otherwise, if the corresponding ElementValue is an ElementValueArrayInitializer, then the array value represented by the ElementValueArrayInitializer is associated with the element.

在评论中澄清上述内容:

With a comment clarifying the above stating:

换句话说,当单元素数组与数组值注释类型元素相关联时,允许省略花括号.

因为 Scala 没有等效的数组初始化语法,你必须使用 Array(elems).

Because Scala has no equivalent array initializer syntax, you must use Array(elems).

这篇关于为什么@RequestMapping注解在java中接受String参数,而在scala中不接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆