Java – Jersey: what does “syntax element not found” mean?

After upgrading Jersey from version 1.15 to 1.17, it starts logging the following messages:

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

Examples of services that generate such messages:

@GET
@Path("/bla/{a}")
@Produces("application/json")
public String doStuff(@PathParam("a") String a) {
    return a;
}

My first impression is that this is an error message, purely based on the wording of the message ("unable to find") However, it is recorded at the info level, and it seems to have no impact in practice because all services continue to work

So my question is whether these log messages indicate a (potential) problem with the way we configure or use Jersey Since the previous version did not happen, I have checked the release notes, but found nothing related

Solution

I have the same "information" message I didn't try to fix its basic Java type (Boolean, string...), but for my own custom class, if I add the @ xmlrootelement annotation and the default no param constructor, the message will disappear

After going deep into the Jersey source code, I noticed that the code of the class "wadlgeneratorjaxbgrammargenerator" is as follows:

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,ex);
} catch (IllegalArgumentException ex) {
    LOGGER.log(Level.FINE,ex);
} catch (InvocationTargetException ex) {
    LOGGER.log(Level.FINE,ex);
} catch (SecurityException ex) {
    LOGGER.log(Level.FINE,ex);
} catch (NoSuchMethodException ex) {
    //getting here for Boolean/String and some other primitive data type
    LOGGER.log(Level.FINE,ex);
}

if (parameterClassInstance==null) {
    return null;
}

So there are basically no string, Boolean and other default constructors, and then it throws nosuchmethodexception, so it returns null and records the information message

So I still don't know why, but in my case, the solution is to disable WADL generation because I don't use it Simply add the following parameters to your web XML is enough

<init-param>
         <param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
         <param-value>true</param-value>
     </init-param>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>