import javax.jws.WebService; /**面向接口的webservice發佈方式 * * */ @WebService public interface JobService { public String getJob(); }
import javax.jws.WebService; @WebService(endpointInterface="cn.it.ws.e.JobService")//設置服務端點接口 ,指定對外提供服務的接口 public class JobServiceImpl implements JobService { @Override public String getJob() { return "JEE研發工程師|Android研發工程師|數據庫工程師|前端工程師|測試工程師|運維工程師"; } public void say(){ System.out.println("早上好!"); } }
import javax.xml.ws.Endpoint; public class Test { public static void main(String[] args) { JobService jobService=new JobServiceImpl(); String address="http://192.168.114.10:9999/ws/jobservice"; Endpoint.publish(address, jobService); System.out.println("wsdl地址:"+address+"?WSDL"); } }
Apache CXF 是一個開源的 Services 框架,CXF 幫助您來構建和開發 Services 這些 Services 能夠支持多種協議,好比:SOAP、POST/HTTP、RESTful HTTP CXF 大大簡化了 Service能夠自然地和 Spring 進行無縫集成。html
CXF介紹 :soa的框架前端
CXF特色java
要想使用CXF框架,那麼就先導入jar包web
接口spring
import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(serviceName="languageManager") public interface LanguageService { public @WebResult(name="language")String getLanguage(@WebParam(name="position")int position); }
實現:數據庫
package cn.it.ws.cxf.a; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; /**開發語言排行描述服務 * * * @author 李俊 2015年5月17日 */ public class LanguageServiceImpl implements LanguageService { /* (non-Javadoc) * @see cn.it.ws.cxf.a.LanguageService#getLanguage(int) */ @Override public String getLanguage(int position){ String language=null; switch (position) { case 1: language="java"; break; case 2: language="C"; break; case 3: language="Objective-C"; break; case 4: language="C#"; break; default: break; } return language; } /**經過cxf框架發佈webservice * 1. ServerFactoryBean * - 不設置註解也能夠發佈webservice服務, 不支持註解 * - 不支持攔截器的添加 * 2. JaxWsServerFactoryBean * - 支持註解 * - 能夠添加攔截器 * 3. webservice 訪問流程: * 1. 檢測本地代理描述的wsdl是否與服務端的wsdl一致 ,俗稱爲握手 * 2. 經過soap協議實現通訊 ,採用的是post請求 , 數據封裝在知足soap規約的xml中 * 3. 返回數據 一樣採用的是soap通訊, 數據封裝在知足soap規約的xml中 * @param args public static void main(String[] args) { LanguageService languageService=new LanguageServiceImpl(); ServerFactoryBean bean=new ServerFactoryBean(); //Endpoint :地址 , 實現對象 bean.setAddress("http://192.168.114.10:9999/ws/cxf/languangeService"); bean.setServiceClass(LanguageService.class);//對外提供webservcie的業務類或者接口 bean.setServiceBean(languageService);//服務的實現bean bean.create();//建立,發佈webservice System.out.println("wsdl地址:http://192.168.114.10:9999/ws/cxf/languangeService?WSDL"); } */ public static void main(String[] args) { LanguageService languageService=new LanguageServiceImpl(); JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean(); //Endpoint :地址 , 實現對象 bean.setAddress("http://192.168.114.10:9999/ws/cxf/languangeService"); bean.setServiceClass(LanguageService.class);//對外提供webservcie的業務類或者接口 bean.setServiceBean(languageService);//服務的實現bean //添加輸入攔截器 :輸入顯示日誌信息的攔截器 bean.getInInterceptors().add(new LoggingInInterceptor()); //添加輸出攔截器 :輸出顯示日誌信息的攔截器 bean.getOutInterceptors().add(new LoggingOutInterceptor()); bean.create();//建立,發佈webservice System.out.println("wsdl地址:http://192.168.114.10:9999/ws/cxf/languangeService?WSDL"); } }
web.xml配置文件:apache
<?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_3_0.xsd" version="3.0"> <display-name>CXF_Server</display-name> <!-- 添加 CXF 的Servlet ,處理 webservice的請求 --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <!-- Spring 監聽添加 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
實體:tomcat
public class Employee { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
接口:服務器
package cn.it.ws.cxf.b; import java.util.List; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import cn.it.ws.cxf.bean.Employee; @WebService(serviceName="EmployeeService") public interface EmployeeManager { void add(@WebParam(name="employee")Employee employee); @WebResult(name="employees")List<Employee> query(); }
接口實現:markdown
package cn.it.ws.cxf.b; import java.util.ArrayList; import java.util.List; import cn.it.ws.cxf.bean.Employee; /**員工管理的業務實現類 * @author 李俊 2015年5月17日 */ public class EmployeeManagerImpl implements EmployeeManager { private List<Employee> employees=new ArrayList<>(); @Override public void add(Employee employee){ //添加到集合中 employees.add(employee); } @Override public List<Employee> query(){ return employees; } }
Spring配置信息:
<?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-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="employeeManagerImpl" class="cn.it.ws.cxf.b.EmployeeManagerImpl"></bean> <!-- 配置cxf 地址: http://192.168.114.10:8080/CXF_Server/ws/employeeManager 組成 : http://192.168.114.10:8080 +CXF_Server( 項目名)+ws(過濾的路徑)+/employeeManager(自定義部分) 服務類 : 服務的實現類: 攔截器 --> <jaxws:server address="/employeeManager" serviceClass="cn.it.ws.cxf.b.EmployeeManager"> <jaxws:serviceBean> <ref bean="employeeManagerImpl"/> </jaxws:serviceBean> <!-- 配置輸入顯示日誌信息的攔截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:server> </beans>
咱們的Intellij idea是一個很是好用的java ide,固然了,它也支持webservice開發。很是好用…因爲在網上見到的教程很是多,我就貼幾個我認爲比較好的教程:
http://www.biliyu.com/article/986.html
http://blog.csdn.net/u010323023/article/details/52926051
http://blog.csdn.net/dreamfly88/article/details/52350370
咱們如今webservice就基本入門了,如今我想要作的就是本身寫的網站可以拿到天氣預報的信息,因而我去http://www.webxml.com.cn/zh_cn/index.aspx找到了天氣預報的服務
這個是天氣預報的WSDL地址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,那麼咱們只要解析該WSDL服務便可
若是不想獲得全部的信息,那麼咱們能夠在服務上找到咱們想要對應的數據,也就是說: