81,092
社区成员
发帖
与我相关
我的任务
分享
package org.my.mycxf;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* CXF Web Service的接口
*
* @author CJY
*
*/
// targetNamespace默认是报名的倒写http://mycxf.my.org/
@WebService
public interface HelloWorld {
@WebMethod(operationName = "SayHello")
@WebResult(name="word")
public String sayHello(@WebParam(name = "name") String name,
@WebParam(name = "address") String address,
@WebParam(name = "email") String email);
}
package org.my.mycxf;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* HelloWorld的实现类
* @author CJY
*
*/
@WebService(endpointInterface="org.my.mycxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@WebMethod(operationName = "SayHello")
@WebResult(name="word")
@Override
public String sayHello(@WebParam(name = "name") String name,
@WebParam(name = "address") String address,
@WebParam(name = "email") String email) {
// TODO Auto-generated method stub
return "你好:"+name+";你的地址是:"+address+";邮箱是:"+email;
}
}
package org.my.mycxf;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsDynamicClientFactory factory=JaxWsDynamicClientFactory.newInstance();
Client client=factory.createClient("http://localhost:8080/MyCXFAnnotation/ws/helloService?wsdl");
QName qName=new QName("http://mycxf.my.org/","SayHello");
try {
Object[] result=client.invoke(qName, new Object[]{"张三","青岛","asdf@qq.com"});
System.out.println(result[0].toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在客户端项目中右键项目名称properties>resource>text file encoding>other>UTF-8
然后项目重新编译一下问题就解决了
另外请确定服务端的jar包是否齐全如果不齐会报错:Cannot create a secure XMLInputFactory package org.my.mycxf;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>sayHello complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType name="sayHello">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="age" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="address" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", propOrder = {
"name",
"age",
"address"
})
public class SayHello {
protected String name;
protected String age;
protected String address;
/**
* 获取name属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* 设置name属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* 获取age属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getAge() {
return age;
}
/**
* 设置age属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setAge(String value) {
this.age = value;
}
/**
* 获取address属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getAddress() {
return address;
}
/**
* 设置address属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setAddress(String value) {
this.address = value;
}
}