cxf 发布 一个简单的 webservice
一個 簡單的 cxf 發布webservice的例子 ,希望能對你有所幫助。
1,開發環境 ? eclipse ? jdk 1.7 ??apache-cxf-3.1.6
2,開發步驟
? 1). ?導入cxf ?jar包 ?我是全部jar包導入了 ,因為 不確定 要哪幾個包
? ? ? ? ? 導入spring 的最小必要包,cxf中 有這些包 ,所以 就不必再添加了?
? 2).寫一個接口 ?我寫的 是 ?IUserService 接口 代碼如下?
package com.wa.webservice.service;
import java.util.List;
import javax.jws.WebService;
import com.wa.webservice.domian.User;
@WebService
public interface IUserService {
public User getUserById(long id);
public List<User> getAllUser();
}
3). 編寫實現類,實現剛才的那個接口,UserService ?實現 IUerService 代碼如下:
package com.wa.webservice.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.wa.webservice.domian.User;
import com.wa.webservice.service.IUserService;
@WebService(endpointInterface = "com.wa.webservice.service.IUserService", serviceName = "UserService")
public class UserServiceImpl implements IUserService {
@Override
public User getUserById(long id) {
User u = new User("sa", 1, 12);
if (id == 1) {
return u;
}
return null;
}
@Override
public List<User> getAllUser() {
List<User> list = new ArrayList<User>();
User u = new User("dd", 1, 152);
User u1 = new User("saa", 2, 132);
User u2 = new User("ss", 3, 122);
list.add(u2);
list.add(u1);
list.add(u);
return list;
}
}
4). 在web.xml文件中 增加spring ,cxf 支持,代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springcxfserver02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 設置Spring容器加載配置文件路徑 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<!-- 加載Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
5). 編寫測試類Test1,代碼如下:
package com.wa.junit;
import javax.xml.ws.Endpoint;
import com.wa.webservice.service.impl.UserServiceImpl;
public class Test1 {
public static void main(String[] args) {
String address = "http://localhost:9000/UserService";
UserServiceImpl implementor = new UserServiceImpl();
Endpoint.publish(address, implementor);
}
}
6).運行測試類,在瀏覽器中輸入如下地址:http://localhost:9000/UserService?wsdl?
出現一個如下的xml文件 則發布成功
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.webservice.wa.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.webservice.wa.com/" name="UserService" targetNamespace="http://impl.service.webservice.wa.com/">
<wsdl:import location="http://localhost:9000/UserService?wsdl=IUserService.wsdl" namespace="http://service.webservice.wa.com/">
</wsdl:import>
<wsdl:binding name="UserServiceSoapBinding" type="ns1:IUserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUserById">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUserById">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserByIdResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getAllUser">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getAllUser">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getAllUserResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserService">
<wsdl:port binding="tns:UserServiceSoapBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:9000/UserService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
?一個 webservice就這樣發布成功了!!!如果您覺得此文章對您有所幫助,就點個贊吧!!!
?
轉載于:https://www.cnblogs.com/Zhong-Xin/p/5422000.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的cxf 发布 一个简单的 webservice的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android手势识别
- 下一篇: 转jmeter --JDBC请求