javascript
Spring 组cxf宣布webservice
通過(guò)spring宣布webservice接口
spring jar包+cxf jar Java包 下列文件丟失jar包需要下載自己
項(xiàng)目結(jié)構(gòu)
1、實(shí)體類
package com.test.entity;public class User {public User(String name, String pwd, String sex) {super();this.name = name;this.pwd = pwd;this.sex = sex;}private String name;private String pwd;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}
2、webservice接口
package com.test.webservice;import java.util.List;import javax.jws.WebService; import com.test.entity.User;/*** 聲明 webService 接口* @author Administrator**/ @WebService public interface IWebservice {public String say(String str);public String findList(); } 3、接口實(shí)現(xiàn)
package com.test.webservice.imp;import java.util.ArrayList; import java.util.List;import javax.jws.WebService;import net.sf.json.JSONArray;import com.test.entity.User; import com.test.webservice.IWebservice;/*** 聲明這個(gè)類是 :IWebservice實(shí)現(xiàn)類* @author Administrator**/@WebService(endpointInterface="com.test.webservice.IWebservice") public class WebServiceImpl implements IWebservice {public String say(String str) {return str;}public String findList(){List<User> u=new ArrayList<User>();u.add(new User("張三", "12343", "男"));u.add(new User("妮妮", "12343", "女"));u.add(new User("利弊", "9900000", "女"));JSONArray json=JSONArray.fromObject(u);return json.toString();} }
4、cxf-service.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" 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-servlet.xml" /> --> <!-- id 自己定義 serviceClass接口完整包名 address訪問(wèn)地址 --> <jaxws:server id="webservice" serviceClass="com.test.webservice.IWebservice" address="/webservice"> <jaxws:serviceBean> <!-- 實(shí)現(xiàn)類 --> <bean class="com.test.webservice.imp.WebServiceImpl" /> </jaxws:serviceBean> </jaxws:server> </beans>
5、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>cxf_service</display-name><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/config/cxf-service.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>/services/*</url-pattern> </servlet-mapping> </web-app>
6、client調(diào)用接口
package com.test.web;import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsClientFactoryBean; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.test.webservice.IWebservice;public class TestWebService {public static void main(String[] args) {//調(diào)用WebService// JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); // // factory.setServiceClass(IWebservice.class); // // factory.setAddress("http://localhost:8080/cxf_service/services/webservice"); // // IWebservice service = (IWebservice) factory.create(); // System.out.println("#############Client getUserByName##############"); // System.out.println(service.say("ABC"));//創(chuàng)建JaxWsDynamicClientFactory 實(shí)例JaxWsDynamicClientFactory fdc=JaxWsDynamicClientFactory.newInstance();//獲取鏈接Client client= fdc.createClient("http://localhost:8080/cxf_service/services/webservice?
wsdl"); //調(diào)用接口名稱 上傳參數(shù) 返回結(jié)果 Object[] obj=null; Object[] objs=null; try { //運(yùn)行指定方法 say 方法名 參數(shù) obj = client.invoke("say","我叫張三"); //運(yùn)行制定方法 findList 方法名 objs = client.invoke("findList"); System.out.println(obj[0].toString()); System.out.println(objs[0].toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
轉(zhuǎn)載于:https://www.cnblogs.com/gcczhongduan/p/5027433.html
總結(jié)
以上是生活随笔為你收集整理的Spring 组cxf宣布webservice的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《第一行代码》学习笔记24-持久化技术(
- 下一篇: 折纸比山高