简单cxf+spring构建webservice服务
@XmlType(name="StudentInfo")@XmlAccessorType(XmlAccessType.FIELD)public class Student implements Serializable{private static final long serialVersionUID = 6841706329341519463L;private String id;private String name;private Integer age;private String addr;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;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}public static long getSerialVersionUID() {return serialVersionUID;}} @WebServicepublic interface IStudentService {public void saveStu(@WebParam(name="StudentInfo") Student student,@WebParam(name="saveFlag")boolean flag);public void updateStu(@WebParam(name="StuInfo")String info);public void deleteStu(@WebParam(name="id")String id);} @WebServicepublic class StudentServiceImpl implements IStudentService{public void deleteStu(String id) {System.out.println("delete Student Id="+id);}public void saveStu(Student student, boolean flag) {System.out.println("save Student !");}public void updateStu(String info) {System.out.println("Update Student Infor!");}} <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <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="stuService" implementor="com.zzn.serviceImpl.StudentServiceImpl" address="/stuService" /> </beans>
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/bean.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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>/*</url-pattern></servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
C:\Users\zzn>wsdl2java -frontend jaxws21 -d e:\ -p com.zzn.service http://localhost:8080/HelloCXF/service?wsdl
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="service" address="http://localhost:8080/HelloCXF/stuService" service/> </beans>
public class TestClient {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");IStudentService service = (IStudentService)context.getBean("service");StudentInfo studentInfo = new StudentInfo();studentInfo.setAddr("China");studentInfo.setAge(20);studentInfo.setId("sd2011022");studentInfo.setName("zhangzhennan");service.saveStu(studentInfo, true);service.deleteStu("sd2011022");}}