简体   繁体   English

如何在Java运行时执行typecastng?

[英]how to perform typecastng at runtime in java?

i am trying to make a client for services exposed through REST.I have multiple number of classes extending a single class. 我试图为通过REST公开的服务创建客户端。我有多个类扩展了一个类。

Now when i send the request and get a response,every time i need to type cast the response for specific class.i am trying to automate this process,can this be achieved at run time? 现在,当我发送请求并获得响应时,每次我都需要键入将响应转换为特定类。我正在尝试使该过程自动化,这可以在运行时实现吗?

I am thinking of using generics and reflection but unable to move forward.what i exactly want to achieve is by just mentioning a unique string or say request,i must be able to get exact same response without type casting it with that particular response class. 我正在考虑使用泛型和反射,但无法前进。我真正想要实现的是仅提及一个唯一的字符串或说请求,我必须能够获得完全相同的响应,而无需使用该特定响应类进行类型转换。

By using generics i succeeded in reducing some code for typecasting,still i am not satisfied as i want to completely achieve it at run time. 通过使用泛型,我成功地减少了一些用于类型转换的代码,但仍然不满意,因为我想在运行时完全实现它。

RequestClass request=(RequestClass)getRequest(some attributes);
output=(Responseclass)response.getResult();

Here every time i need to mention the request and response classes,i don't want to do this. 这里每次我需要提及请求和响应类时,我都不想这样做。

can i do something where i can map the request and response classes to a key or a string and based on it the code will fetch the request and response class and perform the operation according to it(not sure about it). 我可以做些我可以将请求和响应类映射到键或字符串的事情吗,并基于此代码将获取请求和响应类并根据它执行操作(不确定)。

please guide me in doing this,or any other way i can do the above mentioned thing. 请引导我执行此操作,或者以其他任何方式可以执行上述操作。 Thanks in advance. 提前致谢。

Consider using one of the many Java REST libraries. 考虑使用许多Java REST库之一。 Our client uses the Jersey API to handle requests and responses to our Python based RESTful server. 我们的客户使用Jersey API处理对基于Python的RESTful服务器的请求和响应。

Jersey uses a class called ClientResponse that stores the generic response data. 泽西岛使用称为ClientResponse的类来存储常规响应数据。 you can use the getEntity method to return the response as a specific type. 您可以使用getEntity方法将响应作为特定类型返回。

Here is an excerpt of my code, which only deals with strings, but you can see how it could be extended: 这是我的代码的摘录,它仅处理字符串,但是您可以看到如何扩展它:

    ClientResponse response; //a Jersey class
    String responseText;
    WebResource odbc = resourceCollection.path("ODBC"); //another Jersey class
    try {
        //we send a POST and get a response stored as generic
        response = odbc.type(media)
                .accept(MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN)
                .post(ClientResponse.class, formdata);
        //we pull out the response entity as a string
        responseText = response.getEntity(String.class);
    } catch (UniformInterfaceException e) {
        write("<UniformInterfaceException>\n");
        write("  Response type was not expected\n");
        write("</UniformInterfaceException>\n");
        return;         
    } catch (ClientHandlerException e) {
        write("<Error>\n");
        write("  Unable to connect or connection refused\n");
        write("</Error>\n");
        return;
public class DynamicCasting{
    DynamicCasting e1=new DynamicCasting();
    private Object obj=new Object();
    DynamicCasting.doSomething(obj);
    public static DynamicClass doSomething(DynamicClass dynClassObject){
        return dynClassObject;
    }
}

The obj will be type casted to DynamicClass in this example.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM