用Maven生成JAX-WS调用stub代码(用于访问WebService)

请注意例子中myservice.wsdl需要先从http://localhost:8080/TestWS/myservice?wsdl导出后放到src/wsdl


Use Maven to Generate all Stubs using "wsimport" utility . After generating the webservice you can call the service using Java class. This method will can be easily reused , you just have to use different wsdl url every time you have to consume the webservice and modify the java code , it's light weight and no need to use any third party libraries . So you will need the following

  • wsdl file
  • pom.xml
  • Java Class

First step is to copy the wsdl file and place it in the src/wsdl directory
Setup the pom.xmlpom.xml
Folder : Project root dir
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  < modelVersion >4.0.0</ modelVersion >
  < groupId >com.myws.testconsumews</ groupId >
  < artifactId >TestWSConsume</ artifactId >
  < packaging >war</ packaging >
  < version >1.0-SNAPSHOT</ version >
 
  < name >Test Webservice</ name >
  < url >http://maven.apache.org</ url >
  
  
  < build >
   
   < plugins >
    
    < plugin >
     < groupId >org.codehaus.mojo</ groupId >
     < artifactId >jaxws-maven-plugin</ artifactId >
     < version >1.7</ version >
     < executions >
      
       < execution >
       < id >generate-stubs</ id >
       < phase >generate-sources</ phase >
       < goals >
        < goal >wsimport</ goal >
       </ goals >
       < configuration >
           < wsdlDirectory >src/wsdl</ wsdlDirectory >
        < wsdlFiles >
         < wsdlFile >myservice.wsdl</ wsdlFile >
        </ wsdlFiles >
        < wsdlLocation >http://localhost:8080/TestWS/myservice?wsdl</ wsdlLocation >
        < sourceDestDir >src/main/java</ sourceDestDir >
                        < packageName >com.myservice.proxy</ packageName >
                   </ configuration >
        
      </ execution
     </ executions >
    </ plugin >
    < plugin >
     < groupId >org.apache.maven.plugins</ groupId >
     < artifactId >maven-war-plugin</ artifactId >
     < version >2.2</ version >
      < configuration >
      < failOnMissingWebXml >false</ failOnMissingWebXml >
     </ configuration >
    </ plugin >
 
   </ plugins >
  </ build >
</ project >

run "mvn generate-sources" command , this will generate the files in src/main/java folder . Its specified as <sourceDestDir>src/main/java</sourceDestDir> in pom.xml
Stubs are generated ,You can call the webservice with following Java program
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.myservice.client;
 
import com.myservice.proxy.MyService;
import com.myservice.proxy.MyServiceService;
 
public class CallMyService {
  
  
  
  public static void main(String[] args) {
   MyServiceService mss = new MyServiceService();
   MyService serv = mss.getMyServicePort();
    
      System.out.println(serv.sayHello());
  }
  
  
   
}
Now that was simple right . So what if you want to generate numerous webservice clients for testing and scripting needs you can simply reuse the pom.xml ( change the wsdl file ) , generate the stubs and change the program to call the new webservice .
Note
If your webservice requires authentication , you can set that up as shown below
?
1
2
3
((BindingProvider) serv).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);
  ((BindingProvider) serv).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, passWord);
     
Cheers, hope that was helpful
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值