开发者

NetBeans webservice client stubs - incompatible type?

I generated my client side stubs for a NetBeans webservice.

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.

i.e. (note the packages)

package adiib.ws.harmoniser;

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

The POJO LogMessage reads:

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 excep开发者_开发知识库tionClassName) {
        this.exceptionClassName = exceptionClassName;
    }

    public String getThrowerClassName() {
        return throwerClassName;
    }

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

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


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.


you right, generated class for the stub are images of the POJO classes. They are generated to transfer data from remote server.

You have to use setter/getter and adapt data on your POJO.

Your method need to be wrapped in another method which belong to a service class. (call it MyClassServiceImpl)

and call the method in your application implementation.


Add the following to your LogMessage class (in the service):

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "LogMessage")
    public class LogMessage implements Serializable 
    { ... }

Rebuild the server. In your client application, go to Web Service References and Right Click -> Refresh... the service.

The LogMessage class that you want to use will then appear in the Generated Sources folder of the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜