webService接口开发案例

一、开发webservice接口的方式

1、使用jdk开发

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

二、使用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。

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值