阅读@RequestMapping文档:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html
它接受String数组参数用于其路径映射.
所以这适用于java:
@RequestMapping("MYVIEW")
但在scala我需要使用:
@RequestMapping(Array("MYVIEW"))
scala版本有意义,因为注释需要String数组.但是为什么上面的java工作,如果不给出编译时错误?
下面的’ArrayChecker’类(我编写的一个类来说明这一点)会导致java编译时错误:
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”)引起类似的错误?
解决方法:
Java SE规范的第9.7.1节规定:
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.
通过评论澄清上述说明:
In other words, it is permissible to omit the curly braces when a single-element array is to be associated with an array-valued annotation type element.
由于Scala没有等效的数组初始值设定语法,因此必须使用Array(elems).
原文地址:https://codeday.me/bug/20190517/1123392.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。