Cannot use variables defined in classes in groovy annotations

I am trying to port some code from Dropwizard examples from java to groovy.

I see that in java I can use the following code without any problems:

package com.example.helloworld;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldService{  

}

However, using the groovy compiler (both 1.8 and 2.0.6), the class does not compile with noClassFoundException around MediaType.APPLICATION_JSON

If I changed this code to use the actual string value

@Produces('application/json')
public class HelloWorldService{  

}

everything works perfectly.

Are there any differences between how groovy resolves annotations and the way java works?

For completeness, this is part of the gradle project, and here is my build.gradle (the file goes under src / groovy / com / example / helloworld)

apply plugin: 'groovy'

// Set our project variables
project.ext {
    dropwizardVersion = '0.6.1'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.yammer.dropwizard', name: 'dropwizard-core', version: dropwizardVersion
    groovy group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.8.7'
}

Compilation Error:

: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl... 17 more : java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl at org.gradle.api.internal.tasks.compile.TransformingClassLoader.findClass(TransformingClassLoader.java:47)

+5
1

Groovy, : . , . ( ) .

Groovy javax.ws.rs.core.MediaType , , com.sun.ws.rs.ext.RuntimeDelegateImpl Class.forName ( ), , , . ( , Groovy, , , , .) , :

dependencies {
    compile "com.sun.jersey:jersey-client:1.15" 
}

, Eclipse Groovy , . , GMaven , Gradle, Eclipse ( Gradle).

+6

All Articles