我有一个没有像这样定义的泛型的方法:
private void methodWithoutGenerics(Integer subjectKey) {
Client client = AuthClient.create();
List<JsonResult> clinicalItems = client
.resource(getBaseUrl() + "/rest/v1/patients/" + subjectKey + "/" + "results")
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(new GenericType<List<JsonResult>>() {
});
assertFalse(clinicalItems.isEmpty());
JsonResult clinicalItem = client
.resource(getBaseUrl() + "/rest/v1/patients/" + subjectKey + "/" + "results" + "/" + clinicalItems.get(0).getClinicalItemKey())
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(new GenericType<JsonResult>() {
});
assertNotNull(clinicalItem);
}
此代码正在运行,但我希望能够使用不同的类型来调用它,如下所示:
methodWithGenerics(subjectKey, "results", JsonResult.class);
methodWithGenerics(subjectKey, "medications", JsonMedication.class);
methodWithGenerics(subjectKey, "allergies", JsonAllergy.class);
而且我想不出一种方法来泛化上述方法,以便它与这个签名“工作”。我担心我将不得不复制并粘贴输入内容三遍。
右侧的所有这些类都有一个名为JsonClinicalItem
. 它有一种方法,我调用的临床项目名为getClinicalItemKey()
.
我怎样才能将其泛化?甚至可能吗?
这是我尝试过的:
private <T extends JsonClinicalItem> void methodWithGenerics(Integer subjectKey, String pluralName, Class<T> t) {
Client client = AuthClient.create();
List<T> clinicalItems = client
.resource(getBaseUrl() + "/rest/v1/patients/" + subjectKey + "/" + pluralName)
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(new GenericType<List<T>>() {
});
assertFalse(clinicalItems.isEmpty());
T clinicalItem = client
.resource(getBaseUrl() + "/rest/v1/patients/" + subjectKey + "/" + pluralName + "/" + clinicalItems.get(0).getClinicalItemKey())
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(new GenericType<T>() {
});
assertNotNull(clinicalItem);
}
这可以编译,但是当我运行我的代码时,会methodWithGenerics(subjectKey, "results", JsonResult.class);
抛出一个类转换异常。很明显,我编写此通用方法的方式与原始方法的外观有所不同。话虽如此,我已经比较了两个调用之间的服务器输出,它们是相同的。这是堆栈跟踪:
java.lang.ClassCastException: java.lang.reflect.Method cannot be cast to java.lang.Class
at com.owlike.genson.reflect.TypeUtil.getTypes(TypeUtil.java:362)
at com.owlike.genson.reflect.TypeUtil.match(TypeUtil.java:298)
at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:102)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:58)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:22)
at com.owlike.genson.Genson.provideConverter(Genson.java:182)
at com.owlike.genson.convert.DefaultConverters$CollectionConverterFactory.create(DefaultConverters.java:115)
at com.owlike.genson.convert.DefaultConverters$CollectionConverterFactory.create(DefaultConverters.java:106)
at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:102)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:58)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:22)
at com.owlike.genson.Genson.provideConverter(Genson.java:182)
at com.owlike.genson.Genson.deserialize(Genson.java:330)
at com.owlike.genson.ext.jersey.GensonJsonConverter.readFrom(GensonJsonConverter.java:124)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:565)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:535)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:696)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:512)
at com.mirth.results.rest.resource.UrlsWorkIntegrationTest.methodWithGenerics(UrlsWorkIntegrationTest.java:114)
at com.mirth.results.rest.resource.UrlsWorkIntegrationTest.canTraverseUrls(UrlsWorkIntegrationTest.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
它在上面写着错误:
List<T> clinicalItems = client
.resource(getBaseUrl() + "/rest/v1/patients/" + subjectKey + "/" + pluralName)
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(new GenericType<List<T>>() {
});
如果我运行 my methodWithoutGenerics
,则不会发生此错误。因此,我不明白的两者之间的泛型必须存在一些差异。
此代码使用泽西岛。我认为这并不重要,因为我的“withoutGenerics”方法工作正常,所以这可能不是泽西岛的问题。但这是我对泽西岛的依赖:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server-linking</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.17.1</version>
</dependency>