java开发webservice

前提:我使用的是spring框架

一、在aaa-servlet.xml中加入1、2、3和4
在beans中加入1和 2

1、xmlns:jaxws=" http://cxf.apache.org/jaxws "
2、xsi:schemaLocation="http://cxf.apache.org/jaxws 
                http://cxf.apache.org/schemas/jaxws.xsd"

(加入是为了能使用jaxws标签)
3、 

< import resource="classpath*:META-INF/cxf/cxf.xml"/ >
< import resource="classpath*:META-INF/cxf/cxf-servlet.xml"/ >
4、

< jaxws:endpoint id="cxfService" address="/CxfSer"
       implementor="com.qnoa.webservice.CxfServiceImpl"/ >

(implementor 注入的是一个继承接口的类(自定义的,类名是CxfServiceImpl)

com.qnoa.webservice是自定义的包名,红色文字都是自定义的。

二、在web.xml中加入5、6、7和8
5、定义上下文

< context-param >  
        < param-name >contextConfigLocation< /param-name >
        < param-value >
          WEB-INF/aaa-servlet.xml
        < /param-value >
< /context-param > 
6、监听上下文
<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
7、(因为用的是apache的解决方案CFX,所以必须使用 CXFServlet 这个名字)
<servlet>
       <servlet-name >CXFServlet</servlet-name>
       <servlet-class >org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
       <load-on-startup >2</load-on-startup>
</servlet >
8、   
<servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <url-pattern>/service/* </url-pattern>
</servlet-mapping>

三、java程序部分

9、定义一个接口类

@WebService
public interface CxfService
  {
 public String getSharefileList( @WebParam(name = "userName") String userName);
 }

10、定义方法类

@WebService(endpointInterface = "com.qnoa.webservice.CxfService")
(endpointInterface必须指向接口类)
public class CxfServiceImpl implements CxfService
{
 public String getSharefileList(String userName)
     {  
   String name = userName + "123";
   return name ;
     }
} 

11、测试类

public class CXlin
{
 public static void main(String args[])
 {
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.setServiceClass(CxfService.class);
  factory.setAddress("http://127.0.0.130:8080/qnew/service/CxfSer");
  CxfService cxfService = (CxfService)factory.create();  
  String user = cxfService.getSharefileList("bushi");
 }
}

测试就会得到bushi123

qnew是工程的名字,CxfSer是在aaa-servlet.xml中自定义的,service是在web.xml中自定义的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值