標籤:des style blog http color io os ar java
package service;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* @description 功能描述:
* @author 作 者: 周志偉
* @param 參 數:
* @createdate 建立日期: 2014-9-4上午9:37:40
* @projectname 項目名稱: spring_mvctype
* @packageclass 包及類名: com.spring.mvc.service.connservice.java
*/
//第一步首先建立web介面
@WebService
public interface connservice {
@WebMethod
public String Stringlist();
@WebMethod
public List getlist();
}
//第二步去實現這個介面
package serviceimpl;
import java.util.List;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.connservice;
import com.spring.mvc.jdbc.JdbcDao;
/**
* @description 功能描述:
* @author 作 者: 周志偉
* @param 參 數:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 項目名稱: spring_mvctype
* @packageclass 包及類名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
@WebService(endpointInterface="service.connservice")
@Service(value="connservice")
public class connserviceimpl implements connservice{
@Autowired
JdbcDao dao;
@Override
/**
* @description 功能描述: webservice 返回list json 結果
* @author 作 者: 周志偉
* @param 參 數:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 項目名稱: spring_mvctype
* @packageclass 包及類名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
public String Stringlist() {
String sql ="select * from CS_CONTRACT t where t.id=‘30053‘";
List list = dao.queryData(sql); // 執行sql
net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(list); //將結果集轉為List json 串
return jsonArray.toString(); //轉為String類型
}
/**
* @description 功能描述: webservice 返回list 結果
* @author 作 者: 周志偉
* @param 參 數:
* @createdate 建立日期: 2014-9-4上午9:38:31
* @projectname 項目名稱: spring_mvctype
* @packageclass 包及類名: com.spring.mvc.serviceimpl.connserviceimpl.java
*/
public List getlist() {
String sql ="select * from CS_CONTRACT t where t.id=‘30053‘";
List list = dao.queryData(sql); //執行sql
List weblist=null;
for (int i = 0; i < list.size(); i++) { //結果集是兩層List 所以得迴圈一層List
weblist = (List) list.get(i);
}
return weblist; //返回結果List
}
}
第三步添加server-ws.xml webservice設定檔 不過得匯入spring cxf jar包
<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xmlns:simple="http://cxf.apache.org/simple"
xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/simple
http://cxf.apache.org/schemas/simple.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 匯入CXF初始配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!--測試web service 介面 -->
<!--此路徑為類的路徑 -->
<bean id="borrowere" class="serviceimpl.connserviceimpl" />
<jaxws:endpoint id="webser" implementor="#borrowere" address="/webser" />
</beans>
第四步在applicationContext.xml 匯入 server-ws.xml
<!-- webservice 設定檔 -->
<import resource="server-ws.xml"/>
第五步在web.xml配置webservice
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
如果有maven 私服可以直接下載jar包
<dependency>
<groupId>org.apache.servicemix.samples.cxf-wsdl-first</groupId>
<artifactId>wsdl-first-cxf-sa</artifactId>
<version>3.3.2</version>
<type>zip</type>
</dependency>
第六步發布項目測試 如果出了此 頁面證明你的webservice介面已經配置成功 如果別人要調你的介面你只需把服務端url給他即可。http://127.0.0.1:9090/spring_mvc/services/webser?wsdl 可以在他本地直接產生用戶端介面進行調用
瀏覽器輸入 http://127.0.0.1:9090/spring_mvc/services/webser?wsdl //webser 為介面名字自訂
第七步調用介面 建立項目
就這樣webservice介面就輕鬆搞定。。。
關於webservice實現web介面