自己用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呢,请懂的大侠和朋友帮忙看看,谢谢拉!
* 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呢,请懂的大侠和朋友帮忙看看,谢谢拉!
解决方案 »
- 如果获取本地时间
- 数据库中字段找不到 怎么办?
- 关于服务器端大文件的存储方式设计的讨论
- WEB项目的实用性!以及客户的可读性!
- 包含很多查询条件,有很多列表框,时间控件的查询适合用jsp来编写吗?
- 关于p6spy+hibernate+tomcat 配置问题
- 关于PreparedStatament的超级简单问题~~~~~~~~~~
- 求助啊! 如何用jfreechart做柱状图和饼图啊?还有其他方法吗?
- 如何读取文件中的某一行的一个数据(文件中有许多行数据)。
- JSP入门问题,请高手指教
- Hibernate 出现attempted to assign id from null one-to-one property
- js 脚本下载问题
里面说到的WSDL文件有更新,但是我这边全部都是自动生成的啊,不存在这种情况。
奇怪的是不知道为什么后台不报错,方法的返回值为null呢。
主要是sayHello这个方法。
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;
}}
@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 = "")
什么意思?WebResult的注解上面不是本来就有吗
@javax.jws.WebResult(name = "return", targetNamespace="")
下面是我帮你找的解决方法:
http://www.eclipse.org/forums/index.php/t/200921/
谢谢,搞定了!非常感谢,在方法上加上WebResult注解然后regenerate service就可以了