webService接口開發案例

一、開發webservice接口的方式

1、使用jdk開發

2、使用第三方工具,如cxf、shiro等

二、使用jdk開發webservice接口

1、服務端編寫一個接口,類加上註解:@WebService  方法名加上註解:@WebMethod

@WebService
public interface Weather {
    @WebMethod
    String queryWeather();
}
@WebService
public class WeatherImpl implements Weather{
    public String queryWeather() {
        return "今日天氣爲晴,偏北風二到三級";
    }
}

 2、寫一個普通的類,使其繼承自spring的上下文監聽器,並在初始化方法中發佈接口,這樣在容器啓動時自動會發布

public class MyListener extends ContextLoaderListener{

    public void contextInitialized(ServletContextEvent event) {
        String address="http://localhost:8080/weather";
        Endpoint.publish(address, new WeatherImpl());
        super.contextInitialized(event);
    }
}

 

web.xml配置:在web容器中設置該監聽器

 <listener>
      <listener-class>springframe.listener.MyListener</listener-class>
  </listener>

三、使用CXF開發webservice接口

1、導入jar包:CXF只需添加這三個jar包的依賴,Maven會自動引入幫我們引入其他jar包

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.4</version>
</dependency>

2、創建服務接口SEI(Service Endpoint Interface),以及接口的實現類

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import com.dmf.sei.UserService;
/**
 * 
 * @author dmf
 * WebService 服務接口SEI(Service Endpoint Interface),在wsdl中就是portType
 *
 */
@WebService
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface UserService {

	public String saveUserInfo(String name,int age);
	public String getUserInfoById(String id);
}
@WebService    //@WebService註解表示該類是一個服務類,需要發佈其中的public的方法
public class UserServiceImpl implements UserService{

	@Override
	public String saveUserInfo(String name, int age) {
		System.out.println("姓名:"+name+" 年齡:"+age);
		return "success";
	}

	@Override
	public String getUserInfoById(String id) {
		System.out.println("用戶id:"+id);
		return "張三";
	}

}

3、發佈這個服務

public class UserServicePublish {

	public static void main(String[] args) {
		UserService userService=new UserServiceImpl();
		JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();
		
		//設置WebService服務地址
		bean.setAddress("http://localhost:9999/UserService");
		//設置WebService服務接口
		bean.setServiceClass(UserService.class);
		//設置WebService服務實現類
		bean.setServiceBean(userService);
		
		//添加輸入攔截器  :輸入顯示日誌信息的攔截器
		//bean.getInInterceptors().add(new LoggingInInterceptor());
		//添加輸出攔截器  :輸出顯示日誌信息的攔截器
		//bean.getOutInterceptors().add(new LoggingOutInterceptor());
		
		bean.create();//發佈webservice
		System.out.println("webservice成功發佈!");
	}
}

4、發佈這個服務測試WebService服務是否發佈成功
在瀏覽器地址欄輸入http://localhost:9999/UserService?wsdl。

 

四、使用CXF整合spring開發webservice接口

 

1、導入jar包:CXF只需添加這三個jar包的依賴,Maven會自動引入幫我們引入其他jar包

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.4</version>
</dependency>

2、創建服務接口SEI(Service Endpoint Interface),以及接口的實現類

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**
 * 
 * @author dmf
 * WebService 服務接口SEI(Service Endpoint Interface),在wsdl中就是portType
 *
 */
@WebService
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface UserService {

	public String saveUserInfo(String name,int age);
	public String getUserInfoById(String id);
}
//@WebService    //@WebService註解表示該類是一個服務類,需要發佈其中的public的方法
@WebService(endpointInterface = "com.dmf.sei.UserService", serviceName = "UserService")  
public class UserServiceImpl implements UserService{

	@Override
	public String saveUserInfo(String name, int age) {
		System.out.println("姓名:"+name+" 年齡:"+age);
		return "success";
	}

	@Override
	public String getUserInfoById(String id) {
		System.out.println("用戶id:"+id);
		return "張三";
	}

}

3、整合spring發佈這個服務

spring.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:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                    http://www.springframework.org/schema/context   
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 
    <import resource="classpath:META-INF/cxf/cxf.xml" />  <!--cxf3.0以後就不需要了 -->
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!--cxf3.0以後就不需要了 -->
 <bean id="UserService" class="com.dmf.sei.impl.UserServiceImpl"></bean>
	<!--#UserService是和你的實現類上聲明的serviceName一致 -->
	<!--id 同上面你聲明的bean id -->
	<!--address 是web.xml上面配置的cxf匹配路徑之後的路徑,隨便起命,但是訪問時要一致-->
	<jaxws:endpoint id="UserService" implementor="#UserService"
		address="/UserService" />
</beans>

 

web.xml   

 <!-- cxf服務-->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping> 

 

4、發佈這個服務測試WebService服務是否發佈成功
在瀏覽器地址欄輸入http://localhost:9999/ws/UserService?wsdl。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章