虽然网上有很多cxf的教程,但还是要自己写写, “好记性不如烂笔头”
1、服务端
1、1 DEMO,用于测试传递对象
package com.xq.model; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="action")
public class Action {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name",length=20, nullable=false)
private String name;
@Column(name="url",length=100, nullable=false)
private String url; public Action(){} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} @Override
public String toString() {
return "Action [id=" + id + ", name=" + name + ", url=" + url + "]";
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Action other = (Action) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (url == null) {
if (other.url != null)
return false;
} else if (!url.equals(other.url))
return false;
return true;
} }
1、2 发布服务的接口
package com.xq.service; import java.util.ArrayList; import javax.jws.WebService;
import javax.jws.soap.SOAPBinding; import com.xq.model.Action; @WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloService {
public String sayHi(String name);
public String findAction(Action action);
public ArrayList<Action> createActions(int i);
}
1、3 接口实现
package com.xq.service.impl; import java.util.ArrayList; import javax.jws.WebService; import com.xq.model.Action;
import com.xq.service.HelloService; @WebService(endpointInterface = "com.xq.service.HelloService", serviceName = "helloService", portName = "helloServicePort")
public class HelloServiceImpl implements HelloService { @Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return "Hi! " + name;
} @Override
public String findAction(Action action) {
// TODO Auto-generated method stub
System.out.println("invock getAction...");
System.out.println(action);
return action.getName(); } @Override
public ArrayList<Action> createActions(int i) {
ArrayList<Action> actions = new ArrayList<Action>();
for (int j = 0; j < i; j++) {
Action action = new Action();
action.setId(new Long(j));
actions.add(action);
}
return actions;
} }
1、4 配置spring
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="helloService" address="/helloService"
implementor="com.xq.service.impl.HelloServiceImpl">
<!-- 加入消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
1、5 配置web.xml
<!-- spring start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring end --> <!-- cxf start -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- cxf end -->
2、客户端
用wsdl2java命令生成客户端 wsdl2java http://localhost:8080/cxftest/ws/helloService?wsdl 文件默认生成到wsdl2java.bat目录,如果自定义目录,可以用wsdl2java -d src -client wsdl文件/wsdl的url,目录如下
2、1 写测试类,直接用main方法
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloService.class);
factory.setAddress("http://localhost:8080/cxftest/ws/helloService?wsdl");
HelloService client = (HelloService) factory.create();
// 以上语句的功能 可以通过spring来实现
ActionArray actions = client.createActions(5);
for (int i = 0; i < actions.getItem().size(); i++) {
System.out.println(actions.getItem().get(i).getId());
} }
查看控制台
十月 27, 2014 11:51:41 上午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.xq.com/}HelloServiceService from class com.xq.service.HelloService
0
1
2
3
4