使用MaxMind java类与ColdFusion [英] Using MaxMind java class with ColdFusion

查看:450
本文介绍了使用MaxMind java类与ColdFusion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ColdFusion的MaxMind java库。

I'm trying to use MaxMind java library with ColdFusion.

我开始在官方的MaxMind网站上转换这个示例代码:

I start converting this sample code on official MaxMind site:

// A File object pointing to your GeoIP2 or GeoLite2 database
File database = new File("/path/to/GeoIP2-City.mmdb");

// This creates the DatabaseReader object, which should be reused across
// lookups.
DatabaseReader reader = new DatabaseReader.Builder(database).build();

InetAddress ipAddress = InetAddress.getByName("128.101.101.101");

// Replace "city" with the appropriate method for your database, e.g.,
// "country".
CityResponse response = reader.city(ipAddress);

Country country = response.getCountry();

我试过的是:

var file = "pathto\maxmind\GeoLite2-City.mmdb";
var db = createObject("java","java.io.File").init(file);

var mm = createObject("java", "com.maxmind.geoip2.DatabaseReader")
.Builder(db)
.build();

dump(c);abort;

我遇到此错误:

Type: java.lang.NoSuchMethodException 
Messages: No matching Method for Builder(java.io.File) found 
for com.maxmind.geoip2.DatabaseReader

我做错了什么?

推荐答案

更新: @oschwald已提供答案,但是,我将此留作扩展注释,因为它包含一些有关从CF访问内部类和构造函数的有用细节)。

(Update: @oschwald already provided the answer. However, I am leaving this as an extended comment as it contains some helpful details about accessing inner classes and constructors from CF)


DatabaseReader reader = new DatabaseReader.Builder(database).build();

c $ c>。在新类名声明中?这表明Builder是一种特殊类型的类。它是DatabaseReader的嵌套或内部类,因此您需要使用特殊语法来创建它的实例,即 createObject(java,path.OuterClass $ InnerClass)

Notice the . in the new class name statement? That indicates Builder is a special type of class. It is a nested or inner class of DatabaseReader, so you need to use a special syntax to create an instance of it, ie createObject("java", "path.OuterClass$InnerClass").

此外,新的DatabaseReader.Builder(数据库)调用Builder类的构造函数来创建一个新的实例。 CF不支持带java对象的new关键字。而应使用 psuedo方法 init() 调用构造函数:

Also, new DatabaseReader.Builder(database) calls a constructor of the Builder class to create a new instance. CF does not support the "new" keyword with java objects. Instead, use the psuedo method init() to call the constructor:

var mm = createObject("java", "com.maxmind.geoip2.DatabaseReader$Builder").init(db).build();

注意:调用 init code>仅当调用类构造函数与一个或多个参数,如这里的情况。如果java代码改为使用默认的无参构造函数 new DatabaseReader.Builder()。build(),你可以在技术上省略对 init()。如果需要,当调用第一个非静态方法(即 build())时,CF自动调用无参构造函数。

NB: Calling init() explicitly is only required when invoking a class constructor with one or more arguments, as is the case here. If the java code was instead using the default, no-argument constructor ie new DatabaseReader.Builder().build(), you could technically omit the call to init(). CF automatically invokes the no-argument constructor if needed, when the first non-static method - ie build() - is invoked.

这篇关于使用MaxMind java类与ColdFusion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆