自己用CXF框架写一个HelloWorld程序,服务端和客户端都是用Eclipse自动生成的。然后直接运行客户端的测试代码:package net.nysit.webservice.impl;/**
 * Please modify this class to meet your needs
 * This class is not complete
 */import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;/**
 * This class was generated by Apache CXF 2.4.2
 * 2012-05-21T15:32:19.203+08:00
 * Generated source version: 2.4.2
 * 
 */
public final class HelloWorldImpl_HelloWorldImplPort_Client {    private static final QName SERVICE_NAME = new QName("http://impl.webservice.nysit.net/", "HelloWorldImplService");    private HelloWorldImpl_HelloWorldImplPort_Client() {
    }    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = HelloWorldImplService.WSDL_LOCATION;
        if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
            File wsdlFile = new File(args[0]);
            try {
                if (wsdlFile.exists()) {
                    wsdlURL = wsdlFile.toURI().toURL();
                } else {
                    wsdlURL = new URL(args[0]);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
      
        HelloWorldImplService ss = new HelloWorldImplService(wsdlURL, SERVICE_NAME);
        HelloWorldImpl port = ss.getHelloWorldImplPort();  
        
        {
        System.out.println("Invoking sayHello...");
        java.lang.String _sayHello_arg0 = "_sayHello_arg016054355";
        java.lang.String _sayHello__return = port.sayHello(_sayHello_arg0);
        System.out.println("sayHello.result=" + _sayHello__return);
        }        System.exit(0);
    }}结果就会报一个错,错误信息如下:[javax.xml.bind.UnmarshalException: unexpected element (uri:"http://impl.webservice.nysit.net/", local:"return"). Expected elements are <{}return>]
后来我在网上查了一下解决办法,然后自己重新写了个调用客户端的测试类,代码如下:
public class CaseTest { /**
 * @param args
 */
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost/TestWebservice1/services/HelloWorldImplPort?wsdl");
// 记录入站消息
factory.getInInterceptors().add(new LoggingInInterceptor());
// 记录出站消息
factory.getOutInterceptors().add(new LoggingOutInterceptor()); if (factory.getProperties() == null) {
Map<String, Object> properties = new HashMap<String, Object>();
factory.setProperties(properties);
}
factory.getProperties().put("set-jaxb-validation-event-handler",
"false");
HelloWorldImpl client = (HelloWorldImpl) factory.create();
String result = client.sayHello("张三");
System.out.println(result); }}主要是其中的factory.getProperties().put("set-jaxb-validation-event-handler",
"false")这句可以解决报错问题。
但是sayHello的返回值为null!!!!但是我看Tomcat6制台有如下信息打印出来:
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://impl.webservice.nysit.net/"><ns2:return>你好!张三</ns2:return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
我看到是消息是正常传递也接收了,而且后台没有任何报错了,
为什么方法的返回值为null呢,请懂的大侠和朋友帮忙看看,谢谢拉!

解决方案 »

  1.   

    补充一下:就是解决报错问题的链接是http://blog.ziki.cn/961.html
    里面说到的WSDL文件有更新,但是我这边全部都是自动生成的啊,不存在这种情况。
    奇怪的是不知道为什么后台不报错,方法的返回值为null呢。
      

  2.   

    HelloWorldImpl 这个类拿出来看一下。。
    主要是sayHello这个方法。
      

  3.   

    你好,终于来人了哈,就很简单的测试类
    package net.nysit.webservice.impl;import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.xml.ws.RequestWrapper;
    import javax.xml.ws.ResponseWrapper;import net.nysit.webservice.HelloWorld;@WebService(targetNamespace = "http://impl.webservice.nysit.net/", portName = "HelloWorldImplPort", serviceName = "HelloWorldImplService")
    public class HelloWorldImpl implements HelloWorld { @WebMethod(operationName = "sayHello", action = "urn:SayHello")
    @RequestWrapper(className = "net.nysit.webservice.impl.jaxws.SayHello", localName = "sayHello", targetNamespace = "http://impl.webservice.nysit.net/")
    @ResponseWrapper(className = "net.nysit.webservice.impl.jaxws.SayHelloResponse", localName = "sayHelloResponse", targetNamespace = "http://impl.webservice.nysit.net/")
    public String sayHello(@WebParam(name = "arg0") String name) {
    // TODO Auto-generated method stub
    return "你好!" + name;
    }}
      

  4.   

    @WebMethod(operationName = "sayHello", action = "urn:SayHello")
    @WebResult(targetNamespace = "")
    @RequestWrapper(className = "net.nysit.webservice.impl.jaxws.SayHello", localName = "sayHello", targetNamespace = "http://impl.webservice.nysit.net/")
    @ResponseWrapper(className = "net.nysit.webservice.impl.jaxws.SayHelloResponse", localName = "sayHelloResponse", targetNamespace = "http://impl.webservice.nysit.net/")
    public String sayHello(@WebParam(name = "arg0") String name) {
            // TODO Auto-generated method stub
            return "你好!" + name;
    }加上WebResult(targetNamespace = "")
      

  5.   


    什么意思?WebResult的注解上面不是本来就有吗
      

  6.   

    在试试这个Add 
    @javax.jws.WebResult(name = "return", targetNamespace="")
    下面是我帮你找的解决方法:
    http://www.eclipse.org/forums/index.php/t/200921/
      

  7.   

    应该需要重新发布tomcat 服务端
      

  8.   

    Add @javax.jws.WebResult(name = "return", targetNamespace="")to your to SEI method and regenerate your service.非常感谢这位朋友,不知道regenerate your service该怎样来操作呢
      

  9.   

    我重启TOMCAT了,难道要不工程REMOVE掉在重新发布? 
      

  10.   

    编译 -> 打包 -> 发布deploy
      

  11.   


    谢谢,搞定了!非常感谢,在方法上加上WebResult注解然后regenerate service就可以了