通过spring发布webservice接口
spring jar包+cxf jar Java包 以下文件缺少jar包需要自行去下载
项目结构
1、实体类
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、接口实现
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; /** * 声明这个类是 :IWebservice实现类 * @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访问地址 --> <jaxws:server id="webservice" serviceClass="com.test.webservice.IWebservice" address="/webservice"> <jaxws:serviceBean> <!-- 实现类 --> <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、客户端调用接口
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) { //调用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")); //创建JaxWsDynamicClientFactory 实例 JaxWsDynamicClientFactory fdc=JaxWsDynamicClientFactory.newInstance(); //获取链接 Client client= fdc.createClient("http://localhost:8080/cxf_service/services/webservice?wsdl"); //调用接口名称 上传参数 返回结果 Object[] obj=null; Object[] objs=null; try { //执行指定方法 say 方法名 参数 obj = client.invoke("say","我叫张三"); //执行制定方法 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(); } } }
时间: 2024-10-11 09:48:27