webservice 服务端搭建,可以通过直接创建webservice项目,也可以在普通项目中搭建,本人感觉搭建主要在配置、类代码跟普通代码无太大区别
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.cpp.procurement.projectregister.pojo.GEMForGBESExpertInfoVo;
@WebService(endpointInterface="com.procurement.projectregister.service.IWebserviceTest")
public class WebserviceTest implements IWebserviceTest{
/**
* 业务逻辑的处理(测试时实用)
*/
@Override
public List<GEMForGBESExpertInfoVo> GetExpertInfoList(String message) {
GEMForGBESExpertInfoVo bean = new GEMForGBESExpertInfoVo();
bean.setIdCard("11111");
bean.setName("22222");
bean.setPhone("3333333");
List<GEMForGBESExpertInfoVo> expertInfoVos = new ArrayList<GEMForGBESExpertInfoVo>();
expertInfoVos.add(bean);
return expertInfoVos;
}
}
接口类:
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface IWebserviceTest {
@WebMethod
public List<GEMForGBESExpertInfoVo> GetExpertInfoList(String message);
}
实体类
public class GEMForGBESExpertInfoVo {
/**
* 专家身份证号
*/
private String idCard;
/**
* 专家名称
*/
private String name;
/**
* 电话
*/
private String phone;
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
特别主意:声明属性时一定要私有,使用private ,使用public启动服务时,一直抛异常,说srping 无法service类进行加载,找不到实体类的属性
spring 中xml的配置
在testservice完成了对实体类vo的注入
<?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:p="http://www.springframework.org/schema/p"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.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" />
<bean id="GEMForGBESExpertInfoVo" class="com.procurement.projectregister.pojo.GEMForGBESExpertInfoVo"/>
<jaxws:endpoint id="testService" implementorClass="com.procurement.projectregister.service.WebserviceTest" address="/test-service" >
<jaxws:implementor ref="GEMForGBESExpertInfoVo">
</jaxws:implementor>
</jaxws:endpoint>
</beans>
Spring applicationContext.xml 中引入上述业务xml,也可以直接将上述xml内容写入到spring applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init="false">
<import resource="classpath:com/procurement/projectregister/spring-webservice-test.xml"/>
</beans>
web.xml 配置
webservice地址请求时,是webservcie拦截、由cxf进行处理
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet>
<servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
最后进行部署、地址访问