正如休息派遣文档中所述,rest应用程序路径必须通过常量在GIN模块中配置,此处为"/api/v1":
public class DispatchModule extends AbstractGinModule {
@Override
protected void configure() {
RestDispatchAsyncModule.Builder dispatchBuilder =
new RestDispatchAsyncModule.Builder();
install(dispatchBuilder.build());
bindConstant().annotatedWith(RestApplicationPath.class).to("/api/v1");
}
}我想使"/api/v1“常量在编译时根据构建系统根据目标环境(prod、dev等)设置的环境变量和其他条件(构建工件主版本.)进行解析。
问题是我不依赖于编译时变量。TextResource/CssResource和GWT的延迟绑定在这里都不会有帮助,因为GWT.create()不能在GIN模块中使用。我考虑过的另一个选项是使用自定义生成器,但对于这个非常简单的需求来说,这似乎太复杂了。
你怎么解决这个问题?
发布于 2014-07-10 17:35:27
如果您使用Maven作为您的构建系统,您可以利用模板-maven-plugin生成一个包含在POM文件中定义的静态变量的Java。生成的类将由GWT代码使用。
例如,您可能希望填充一个BuildConstants类模板。
public class BuildConstants {
// will be replaced by Maven
public static final String API_VERSION = "${myapi.version}";
}并使用Maven属性:
<myapi.version>v1</myapi.version>将被汇编成
public class BuildConstants {
// will be replaced by Maven
public static final String API_VERSION = "v1";
}你可以在你的DispatchModule中引用这些常量
bindConstant().annotatedWith(RestApplicationPath.class).to("/api/" + BuildConstants.API_VERSION); 下面是我在项目中使用的模板-maven-plugin的示例配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/java-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/generated-sources/java-templates
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>发布于 2014-07-11 09:10:59
您没有理由不能用一个bindConstant()方法(或者其他@Provides方法,它将允许您使用TextResource和/或延迟绑定,或者其他什么)来代替TextResource。
Asn是一个示例(尽管没有测试),下面的代码使用JSNI从主机页读取值,这使得它依赖于运行时(而不是编译时):
@Provides @RestApplicationPath native String provideRestApplicationPath() /*-{
return $wnd.restApplicationPath;
}-*/;发布于 2014-08-10 02:32:57
按照Thomas的建议和Simon,您甚至可以根据您的maven配置文件绑定不同的根.gwt.xml文件。然后选择适当的Gin模块类,在该类中绑定常量。
这就是我们在GWTP的CarStore配套项目中所做的事情,例如,做表单因素。
https://stackoverflow.com/questions/24682087
复制相似问题