准备:JDK、Eclipse所需要jar可在我的资源中下载
所需要jar可在我的资源中下载
jar包下载(官网)目前2.7.7下载Binary distribution:
http://cxf.apache.org/download.html

下载完成,好了创建项目:
创建项目:

添加jar包:本次使用:

项目目录结构:

代码:
接口类IStudent:
package com.huzhe.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.huzhe.po.Student;
@WebService
public interface IStudent {
String sayHi(@WebParam(name= "name")String name);
List<Student> InsertStudent(@WebParam(name="student,num")Student student,int num);
}
接口实现类StudentImpl
package com.huzhe.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.huzhe.po.Student;
@WebService(endpointInterface="com.huzhe.service.IStudent",serviceName="StudentImplService")
public class StudentImpl implements IStudent {
@Override
public String sayHi(@WebParam(name = "name") String name) {
System.out.println("sayHi called");
return "hi " + name;
}
@Override
public List<Student> InsertStudent(
@WebParam(name = "student,num") Student student, int num) {
System.out.println("向List中添加"+num+"个student");
List<Student> stus = new ArrayList<Student>();
for(int i = 0;i<num;i++){
stus.add(student);
}
return stus;
}
}
po:
package com.huzhe.po;
public class Student {
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
测试类:
package com.huzhe.test;
import javax.xml.ws.Endpoint;
import com.huzhe.service.StudentImpl;
public class ServieTest {
protected ServieTest() throws Exception {
System.out.println("Starting Server");
StudentImpl implementor = new StudentImpl();
String address = "http://localhost:8080/cxf";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new ServieTest();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
启动成功后,访问:http://localhost:8080/cxf?wsdl
如图: