项目结尾公共模块WebService封装

      在杭州出差一年有余,终于结束那个炼狱式的出差生活,心里一个High啊!虽然知道明年还要过去...

      年末公司其他项目组看起来都是硕果累累啊,唯独我们项目组的兄弟都很萎,一是项目没有验收,另一个是我们项目做了一年多却不能拿出来给公司领导show一下(这个主要原因是因为我们做的这个项目是几家公司合伙做的,穿插的东西太多,很多有亮点的东西都无法玩儿,尤其是项目中和权限挂钩的模块)。我们老大想了一想把我和熊磊磊喊过去,吩咐道:项目我们也做了一年多了,我希望你们能够把项目中独立起来的模块抽出来包装成WebService,以供后期使用或者给其他兄弟项目组的使用。把你们认为能够独立的列一个清单,然后想想怎么实现。

     老大的一番话,我感觉很有道理,我们不光可以温习曾经做过的东西,同事也算是一种学习吧。

     谈到WebService我在浙江项目也封装过,不过是使用了WebService框架,需要引入N多东西(这也是我一直比较反感的)。现在我首先想到的是简单,易用。再说,在之前我已经研究过Jax-WS\JAXB,如果使用Jax-WS来做,对我来说也是小case。好了,不扯远了,步入我们的正题吧!

    接下来我们逐步来探讨技术吧。

  首先,打开Eclipse开发工具,创建一个名为GeoWS的Web Project。新建ICityService.java和IPOIManager.java两个接口,这两个接口分别做“城市定位查询”和“兴趣点查询”。接口都是简单的测试,没有仔细设计。

ICityService.java 代码如下:

1
2
3
4
5
6
7
8
package com.geostar.commonws.city;
  
import com.geostar.commonws.city.domain.UserDTO;
  
public interface ICityService {
      public  String  getCityInfo(String cityName);
      public  UserDTO  getUserInfo(UserDTO user);
}

 其中UserDTO是我测试JAX-WS能不能返回自定义的对象,这个对象我采用jaxb注解对象,其代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.geostar.commonws.city.domain;
  
import javax.xml.bind.annotation.XmlRootElement;
  
@XmlRootElement
public class UserDTO {
     private String userName;
     private String password;
     public String getUserName() {
         return userName;
     }
     public void setUserName(String userName) {
         this .userName = userName;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this .password = password;
     }
  
}

 IPOIManager.java 代码如下:

1
2
3
4
5
6
7
package com.geostar.commonws.city;
  
import com.geostar.commonws.city.domain.PoiDTO;
  
public interface IPOIManager {
     public  PoiDTO  getPOINInfo(String name);
}

 其中的PoiDTO与UserDTO类似,也是我测试用的。 代码如下:

 

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
package com.geostar.commonws.city.domain;
  
import javax.xml.bind.annotation.XmlRootElement;
  
@XmlRootElement
public class PoiDTO {
     private String oid;
     private String standardname;
     private long weight;
     private String addressName;
     private String  imageUrl;
     public String getOid() {
         return oid;
     }
     public void setOid(String oid) {
         this .oid = oid;
     }
     public String getStandardname() {
         return standardname;
     }
     public void setStandardname(String standardname) {
         this .standardname = standardname;
     }
     public long getWeight() {
         return weight;
     }
     public void setWeight( long weight) {
         this .weight = weight;
     }
     public String getAddressName() {
         return addressName;
     }
     public void setAddressName(String addressName) {
         this .addressName = addressName;
     }
     public String getImageUrl() {
         return imageUrl;
     }
     public void setImageUrl(String imageUrl) {
         this .imageUrl = imageUrl;
     }
  
}

 接口已经写好了,接下来便是接口实现类了,接口实现类也是很简单的测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.geostar.commonws.city.impl;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
  
import com.geostar.commonws.city.ICityService;
import com.geostar.commonws.city.domain.UserDTO;
  
@WebService (targetNamespace = "http://www.geostar.com.cn/geoglobe/" , serviceName = "CityService" , portName = "CityServicePort"
public class CityServiceImpl implements ICityService{
     @WebMethod (action= "http://www.geostar.com.cn/geoglobe/CityService/getCityInfo" , operationName= "getCityInfo" )
     public  String  getCityInfo( @WebParam (name= "cityName" )String cityName){
         return "浙江" ;
     };
     @WebMethod (action= "http://www.geostar.com.cn/geoglobe/CityService/getUserInfo" , operationName= "getUserInfo" )
      public  UserDTO  getUserInfo(UserDTO user){
          UserDTO u= new UserDTO();
          u.setPassword(user.getPassword());
          u.setUserName(user.getUserName());
          return u;
      };
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.geostar.commonws.city.impl;
  
import javax.jws.WebMethod;
import javax.jws.WebService;
  
import com.geostar.commonws.city.IPOIManager;
import com.geostar.commonws.city.domain.PoiDTO;
@WebService (targetNamespace = "http://www.geostar.com.cn/geoglobe/" , serviceName = "POIManager" , portName = "POIManagerPort"
public class POIManagerImpl implements IPOIManager{
     @WebMethod (action= "http://www.geostar.com.cn/geoglobe/POIManager/getPOINInfo" , operationName= "getPOINInfo" )
     public  PoiDTO getPOINInfo(String name){
          PoiDTO  poi= new PoiDTO();
          poi.setOid( "xxxxxxxxxxxxxxxxxxxx" );
          poi.setAddressName( "浙江省测绘局" );
          poi.setImageUrl( "/image/poi/point.gif" );
          poi.setWeight( 10 );
          return poi;
      };
}

 接口和接口实现类都完了,下面该来写配置文件了,首先来看看jax-ws的配置文件 sun-jaxws.xml(位置在web-inf目录下,下同web.xml)

?
<?xml version= "1.0" encoding= "UTF-8" ?>
<endpoints xmlns= "http://java.sun.com/xml/ns/jax-ws/ri/runtime" version= "2.0" >
     <endpoint    
         name= "POIComponent"  
         implementation= "com.geostar.commonws.city.impl.POIManagerImpl"  
         url-pattern= "/POIManager" />  
          <endpoint    
         name= "CityComponent"  
         implementation= "com.geostar.commonws.city.impl.CityServiceImpl"  
         url-pattern= "/CityQuery" />  
</endpoints>

web.xml的配置文件如下:

 ok,整个工程差不多了,接下来便是让这个工程怎么在tomcat下跑起来了,上面的代码如果编译是能通过的,但是如果想运行起来,还需要在tomcat安装目录下的lib文件夹下丢一些jar包,他们是:

jaxb-impl.jar
jaxws-api.jar
jaxws-rt.jar
gmbal-api-only.jar
management-api.jar
stax-ex.jar
streambuffer.jar
policy.jar

这些jar包 你可以在http://jax-ws.java.net/ 这个网站下载的。

ok,我们可以编译运行了。

如果tomcat启动无错,会显示以下信息:

 那我们可以在浏览器下敲入:http://localhost:8080/GeoWS/POIManager?wsdl 和http://localhost:8080/GeoWS/CityQuery?wsdl 来查看这两个webservice,为了能更好的测试webservice,我们可以下载一个WebService客户端工具,比如我使用SOAPUI来测试,具体这个工具怎么使用,我想聪明的大家三两下能搞定的。测试完毕,我们发现webservice接口可以返回复杂的对象。

   今天就写到这里了,如果有什么疑问可以联系我,我的QQ:570777808。

    转载请注明出处:http://www.cnblogs.com/likehua/archive/2012/01/17/2324643.html

wsimport生成客户端出现的异常

今天用wsimport命令生成WebService客户端代码总是失败,搞的我很郁闷,错误信息如下:

C:\Users\DELL>wsimport  -d com   -keep   http://10.3.9.86:8080/GeoWS/CityQuery?wsdl
parsing WSDL...


[ERROR] the following naming conflicts occurred: com.geostar.commonws.city.impl.CityServiceImpl
  line 1 of http://10.3.9.86:8080/GeoWS/CityQuery?wsdl

我尝试了wsimport的各个参数,最后无语了。于是,我不得不求助谷大姐,有一个解释比较合理

http://www-01.ibm.com/support/docview.wss?uid=swg1PK61939  于是,我将我的Jax-WS注解中的seviceName改了一下,重新发布服务,最后客户端代码生成成功!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值