前段时间在弄各种框架下的webservice,要弄demo,网上搜罗了许多,都讲得很好,但感觉就是说得很多很复杂,我就想弄个服务出来供我用一下, 要像网上那么做觉着太麻烦,后来参考各路神仙大佬们后,把代码极度缩小,写了个小实例供自个跑着玩,顺便代码贴上,供大伙口水
支持包:cxf框架lib下核心包,自己官网上下去,在这里就不贴了,除此之外有java环境1.6+就Ok了,其它略过,上代码了。
package com.cxf.bean;
public class Employee {
private String id;
private String name;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.cxf.service;
import javax.jws.WebService;
import com.cxf.bean.Employee;
@WebService
public interface EmployeeService {
public boolean insertEmployee(Employee e);
public boolean updateEmployee(String id);
public boolean deleteEmployee(String id);
public Employee seletctEmployee(String id);
}
package com.cxf.service;
import javax.jws.WebService;
import com.cxf.bean.Employee;
@WebService
public class EmployeeServiceImpl implements EmployeeService {
@Override
public boolean insertEmployee(Employee e) {
//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵
return true;
}
@Override
public boolean updateEmployee(String id) {
//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵
return true;
}
@Override
public boolean deleteEmployee(String id) {
//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵
return true;
}
@Override
public Employee seletctEmployee(String id) {
Employee e = new Employee();
e.setAddress("//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵");
e.setId("//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵");
e.setName("//业务想咋整自已实现去吧,我这里作例子,就直接回true了,呵呵");
return e;
}
}
package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.cxf.service.EmployeeServiceImpl;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(EmployeeServiceImpl.class);
factory.setAddress("http://192.9.11.53:8088/employeeService");
Server server = factory.create();
server.start();
}
}
package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.bean.Employee;
import com.cxf.service.EmployeeService;
public class EmployeeServiceClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://192.9.11.53:8088/employeeService");
factory.setServiceClass(EmployeeService.class);
EmployeeService es = (EmployeeService) factory.create();
Employee e = new Employee();
e= es.seletctEmployee("id");
System.out.println("地址:"+e.getAddress()+" 姓名:"+e.getName()+" 编号:"+e.getId());
System.out.println(es.seletctEmployee("test"));
}
}
//在eclipse里跑发布类后浏览器中访问http://192.9.11.53:8088/employeeService?wsdl得到描述wsdl
<wsdl:definitions name="EmployeeServiceImplService" targetNamespace="http://service.cxf.com/"><wsdl:types><xs:schema elementFormDefault="unqualified" targetNamespace="http://service.cxf.com/" version="1.0"><xs:element name="deleteEmployee" type="tns:deleteEmployee"/><xs:element name="deleteEmployeeResponse" type="tns:deleteEmployeeResponse"/><xs:element name="insertEmployee" type="tns:insertEmployee"/><xs:element name="insertEmployeeResponse" type="tns:insertEmployeeResponse"/><xs:element name="seletctEmployee" type="tns:seletctEmployee"/><xs:element name="seletctEmployeeResponse" type="tns:seletctEmployeeResponse"/><xs:element name="updateEmployee" type="tns:updateEmployee"/><xs:element name="updateEmployeeResponse" type="tns:updateEmployeeResponse"/><xs:complexType name="updateEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="updateEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="insertEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="tns:employee"/></xs:sequence></xs:complexType><xs:complexType name="employee"><xs:sequence><xs:element minOccurs="0" name="address" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:string"/><xs:element minOccurs="0" name="name" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="insertEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="deleteEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="deleteEmployeeResponse"><xs:sequence><xs:element name="return" type="xs:boolean"/></xs:sequence></xs:complexType><xs:complexType name="seletctEmployee"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="seletctEmployeeResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="tns:employee"/></xs:sequence></xs:complexType></xs:schema></wsdl:types><wsdl:message name="insertEmployeeResponse"><wsdl:part element="tns:insertEmployeeResponse" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="updateEmployee"><wsdl:part element="tns:updateEmployee" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="insertEmployee"><wsdl:part element="tns:insertEmployee" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="updateEmployeeResponse"><wsdl:part element="tns:updateEmployeeResponse" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="deleteEmployeeResponse"><wsdl:part element="tns:deleteEmployeeResponse" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="seletctEmployee"><wsdl:part element="tns:seletctEmployee" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="seletctEmployeeResponse"><wsdl:part element="tns:seletctEmployeeResponse" name="parameters">
</wsdl:part></wsdl:message><wsdl:message name="deleteEmployee"><wsdl:part element="tns:deleteEmployee" name="parameters">
</wsdl:part></wsdl:message><wsdl:portType name="EmployeeService"><wsdl:operation name="updateEmployee"><wsdl:input message="tns:updateEmployee" name="updateEmployee">
</wsdl:input><wsdl:output message="tns:updateEmployeeResponse" name="updateEmployeeResponse">
</wsdl:output></wsdl:operation><wsdl:operation name="insertEmployee"><wsdl:input message="tns:insertEmployee" name="insertEmployee">
</wsdl:input><wsdl:output message="tns:insertEmployeeResponse" name="insertEmployeeResponse">
</wsdl:output></wsdl:operation><wsdl:operation name="deleteEmployee"><wsdl:input message="tns:deleteEmployee" name="deleteEmployee">
</wsdl:input><wsdl:output message="tns:deleteEmployeeResponse" name="deleteEmployeeResponse">
</wsdl:output></wsdl:operation><wsdl:operation name="seletctEmployee"><wsdl:input message="tns:seletctEmployee" name="seletctEmployee">
</wsdl:input><wsdl:output message="tns:seletctEmployeeResponse" name="seletctEmployeeResponse">
</wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="EmployeeServiceImplServiceSoapBinding" type="tns:EmployeeService"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="insertEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="insertEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="insertEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="updateEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="updateEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="updateEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="deleteEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="deleteEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="deleteEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="seletctEmployee"><soap:operation soapAction="" style="document"/><wsdl:input name="seletctEmployee"><soap:body use="literal"/></wsdl:input><wsdl:output name="seletctEmployeeResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="EmployeeServiceImplService"><wsdl:port binding="tns:EmployeeServiceImplServiceSoapBinding" name="EmployeeServiceImplPort"><soap:address location="http://192.9.11.53:8088/employeeService"/></wsdl:port></wsdl:service></wsdl:definitions>