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

查看:107
本文介绍了为什么@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

它接受String数组参数作为其路径映射.

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版本很有意义,因为注释需要一个String数组.但是,为什么上面的代码在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") ?

推荐答案

部分

如果元素类型是数组类型,并且对应的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天全站免登陆