NetBeans Web服务客户端存根-不兼容的类型? [英] NetBeans webservice client stubs - incompatible type?

查看:119
本文介绍了NetBeans Web服务客户端存根-不兼容的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为NetBeans Web服务生成了客户端存根.

I generated my client side stubs for a NetBeans webservice.

Webservice实现使用我项目中的本地POJO.生成的存根创建了此POJO的修订版以供使用.使用服务时,我想使用原始的POJO,而不是生成的类型.类型转换无效.

The webservice implementation uses a local POJO from my project. The generated stubs have created a revision of this POJO for use. When I'm using the service, I want to use the original POJO, not the generated type..? Type casting doesn't work.

即(注意包装)

package adiib.ws.harmoniser;

@WebMethod(operationName = "getStartupLogMessages")
public ArrayList<LogMessage> getStartupLogMessages() {
    return startupLogMessages;
}

POJO LogMessage 读取:

package adiib.shared;

public class LogMessage implements Serializable 
{    
    private static final long serialVersionUID = 8379681391654158512L;

    private String exceptionMessage;
    private String customMessage;
    private String stackTrace;
    private LogMessageEnum classification;
    private String effectiveTime;
    private String exceptionClassName;
    private String throwerClassName;

    public LogMessage(){}

    public LogMessage(String exceptionMessage, String customMessage,
        String stackTrace, LogMessageEnum classification, String effectiveTime,
        String exceptionClassName, String throwerClassName)
    {
        this.exceptionMessage = exceptionMessage;
        this.customMessage = customMessage;
        this.stackTrace = stackTrace;
        this.classification = classification;
        this.effectiveTime = effectiveTime;
        this.exceptionClassName = exceptionClassName;
        this.throwerClassName = throwerClassName;
    }

    public String getCustomMessage() {
        return customMessage;
    }

    public void setCustomMessage(String customMessage) {
        this.customMessage = customMessage;
    }

    public String getExceptionMessage() {
        return exceptionMessage;
    }

    public void setExceptionMessage(String exceptionMessage) {
        this.exceptionMessage = exceptionMessage;
    }

    public LogMessageEnum getClassification() {
        return classification;
    }

    public void setClassification(LogMessageEnum classification) {
        this.classification = classification;
    }

    public String getEffectiveTime() {
        return effectiveTime;
    }

    public void setEffectiveTime(String effectiveTime) {
        this.effectiveTime = effectiveTime;
    }

    public String getStackTrace() {
        return stackTrace;
    }

    public void setStackTrace(String stackTrace) {
        this.stackTrace = stackTrace;
    }

    public String getExceptionClassName() {
        return exceptionClassName;
    }

    public void setExceptionClassName(String exceptionClassName) {
        this.exceptionClassName = exceptionClassName;
    }

    public String getThrowerClassName() {
        return throwerClassName;
    }

    public void setThrowerClassName(String throwerClassName) {
        this.throwerClassName = throwerClassName;
    }    
}

现在,在客户端上,当我尝试像这样使用webservice方法时:

Now, on the client side when I'm trying to use the webservice method like so:

package adiib.server;

private void getStartupLogMessages() {

private static List<LogMessage> logMessages = new ArrayList<LogMessage>();

    dsto.adiib.ws.client.harmoniser.AdiibHarmoniser_Service service = new dsto.adiib.ws.client.harmoniser.AdiibHarmoniser_Service();
    dsto.adiib.ws.client.harmoniser.AdiibHarmoniser port = service.getAdiibHarmoniserPort();

    List<dsto.adiib.ws.client.harmoniser.LogMessage> startupLogMessages = port.getStartupLogMessages();
    for (adiib.ws.client.harmoniser.LogMessage logMessage : startupLogMessages) {
        /* 
         * this fails becuase it's looking for adiib.ws.client.harmoniser.LogMessage
         * not adiib.shared.LogMessage; adiib.ws.client.harmoniser.LogMessage is the
         * generated type..
         */
        logMessages.add((LogMessage) logMessage);
    }
}

有什么想法吗?我所能想到的就是创建一种转换方法..这似乎是错误的.

Any ideas? All I can think is creating a conversion method.. that seems wrong.

WulfgarPro

WulfgarPro

推荐答案

该工具生成的类与原始类不同.因此,您必须在客户端使用工具生成的工具与Web服务进行通信.您不能用为服务器端编写的代码替换它.

例如,考虑使用JAX-WS构建的客户端DTO.如果打开源代码,您将看到自动生成的代码(使用wsimport)包含在服务器端类中可能不存在的注释(除非您手动编写).因此,据我了解,您必须使用工具生成的工具.

在调用Web服务之前,您可能必须编写一些方法以将DTO转换为生成的工具.如果生成的类具有相同的属性集(生成客户端DTO时该工具未更改类型和命名),则可能可以使用类似Apache Commons BeanUtils的名称(请参见

The classes generated by the tool are not the same as the original ones you have. So you have to use the tool generated ones in your client side to communicate with the web service. You cannot replace it with the ones you wrote for your server side.

For example, consider JAX-WS built client side DTOs. If you open up the source code, you will see that the auto-generated ones (using wsimport) contains annotations which may not be present (unless you manually wrote) in your server side classes. Therefore, as of my understanding, you have to go with the tool generated ones.

You might have to write methods to transform your DTOs to the tool generated one before web service is invoked. If your generated classes have the same set of properties (type and naming has not been altered by the tool when generating client DTOs), then probably you could use something like Apache Commons BeanUtils (see http://commons.apache.org/beanutils/) to aid in transformation. You can simply call BeanUtils.copyProperties() and pass in your source DTO (your own type) and the target DTO (WS generated) and get it transformed.

这篇关于NetBeans Web服务客户端存根-不兼容的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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