java Jersey:“找不到语法元素”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15767973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey: what does "couldn't find grammar element" mean?
提问by Kees Kist
After upgrading Jersey from version 1.15 to 1.17 it started to log the following messages:
将 Jersey 从 1.15 版升级到 1.17 版后,它开始记录以下消息:
Apr 2, 2013 5:13:06 PM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
An example of a service that produces such a message:
产生此类消息的服务示例:
@GET
@Path("/bla/{a}")
@Produces("application/json")
public String doStuff(@PathParam("a") String a) {
return a;
}
My first impression would be to consider this an error message, purely based on the way the message is phrased ("couldn't find"). However, it's logged at a level of INFO, and it doesn't seem to have any effects in practice since all services continue to work.
我的第一印象是认为这是一条错误消息,完全基于消息的措辞方式(“找不到”)。但是,它记录在 INFO 级别,并且在实践中似乎没有任何影响,因为所有服务都继续工作。
So my question is whether these log messages indicate a (potential) problem with the way we are configuring or using Jersey. Since it didn't happen with the previous version I already checked the release notes, but didn't find anything related.
所以我的问题是这些日志消息是否表明我们配置或使用 Jersey 的方式存在(潜在)问题。由于之前的版本没有发生这种情况,我已经查看了发行说明,但没有找到任何相关内容。
回答by Kim D.
I had the same "info" message as well. I didn't manage to fix it (yet) for basic java types (Boolean, String...) but for my own custom classes if I add the @XmlRootElement annotation and a default no-param constructor the message dissapears.
我也有相同的“信息”消息。我还没有设法为基本的 Java 类型(布尔值、字符串...)修复它,但是对于我自己的自定义类,如果我添加 @XmlRootElement 注释和默认的无参数构造函数,消息就会消失。
Digging into jersey source code I noticed the class "WadlGeneratorJAXBGrammarGenerator" the following code :
深入研究球衣源代码,我注意到“WadlGeneratorJAXBGrammarGenerator”类的以下代码:
Object parameterClassInstance = null;
try {
Constructor<?> defaultConstructor = type.getDeclaredConstructor();
defaultConstructor.setAccessible(true);
parameterClassInstance = defaultConstructor.newInstance();
} catch (InstantiationException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (IllegalAccessException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (IllegalArgumentException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (InvocationTargetException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (SecurityException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (NoSuchMethodException ex) {
//getting here for Boolean/String and some other primitive data type
LOGGER.log(Level.FINE, null, ex);
}
if (parameterClassInstance==null) {
return null;
}
So basically there is no default constructor for String, Boolean and few others then it throws a NoSuchMethodException therefore it return nulls and log the info message.
所以基本上没有 String、Boolean 和其他几个的默认构造函数然后它抛出一个 NoSuchMethodException 因此它返回空值并记录信息消息。
So still no idea why it happens but in my case the solution was to disable the wadl generation since I was not using it. Just add the following param to your web.xml
所以仍然不知道为什么会发生这种情况,但在我的情况下,解决方案是禁用 wadl 生成,因为我没有使用它。只需将以下参数添加到您的 web.xml
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>