开发java版的web service接口

由于经常做跨项目跨平台跨语言的工作,所以web service使用的比较多,但是每次都是别人帮忙写的,自己却还是不会,今天自己也学着写了简单的接口,以后就不用麻烦别人了。

注:这里使用的是CXF,关于CXF的介绍请读者自己百度。

第一步:下载最新的cxf包http://cxf.apache.org/download.html,我下载的是apache-cxf-2.5.2.zip,直接将lib下的所有jar拷贝到lib下 

第二步:编写接口和实现类

    

package bing.server;

import javax.jws.WebService;

@WebService
public interface IHelloService {
	
	public String sayHello(String username);
	
}
package bing.server;

import javax.jws.WebService;

/**
 * <p>
 * WebService实现类
 * </p>
 * 
 * @author wangming
 * @date 2015-2-6
 * @version 1.0
 */
@WebService(endpointInterface = "bing.server.IHelloService", serviceName = "HelloService")
public class HelloServiceImpl implements IHelloService {

    @Override
    public String sayHello(String username) {
        return "hello, " + username;
    }

}

第三步:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>CXFDemo</display-name>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext-server.xml</param-value>
  </context-param>
 
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <display-name>CXFServlet</display-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>/ws/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第四步:配置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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://cxf.apache.org/jaxws 
                http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />  <!-- 这些xml文件在cxf-2.5.0.jar的META-INF目录下 -->
	<!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
		警告提示已经废弃了cxf-extension-soap.xml文件 -->
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


	<!-- 这里配置服务接口,后面描述 id:指在spring配置的bean的ID. Implementor:指明具体的实现类. Address:指明这个web 
		service的相对地址 -->

	<!-- 测试 -->
	<bean id="testServiceImpl" class="bing.server.TestServiceImpl">
	</bean>
	<jaxws:endpoint id="testService" implementor="#testServiceImpl"
		address="/test" />

	<!-- 开启tomcat服务器 ,访问http://localhost:8080/WebExam/services/test?wsdl http://localhost:8080/WebExam是本项目的访问地址 
		services是由于web.xml配置所得,test是由于Spring配置文件中的address属性所得 -->

</beans>

以上过程如果没有出错web service接口也就写好了,这个时候可以去测试一下

转载于:https://my.oschina.net/hnqingping1255/blog/375697

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值