jdk6原生态webservice

近期做cas 单点登录的时候由于要同步用户信息,所以准备在里面加个webservice,开放端口给子系统,于是想用jdk6自带的webservice,使用注解。
开发过程如下:
1,保证使用的是jdk6。
2,编写开放出来的接口并实现,如下

public interface UserService {

public String saveUser(String username,String password);

}



import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.sql.DataSource;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

@WebService(name = "UserService", serviceName = "UserService", targetNamespace = "http://cas.webservice.user/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class UserServiceImpl implements UserService {

private DataSource dataSource;

private SimpleJdbcTemplate jdbcTemplate;

private UserServiceImpl() {

}

private UserServiceImpl(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}

public String saveUser(String username, String password) {
int count = jdbcTemplate.update(
"insert userinfo(userName,password) values(?,?)", username,
password);

return username + " : " + password;
}

}


3,发布webservice。如下

Endpoint.publish("http://127.0.0.1:9527/UserService",
ctx.getBean("wsUserService"));
//注:这里假如不用spring的话 直接new UserServiceImpl()。

4,在你的客户端位置键入命令:

wsimport -keep http://127.0.0.1:9527/UserService?wsdl

此时会在此目录生成cas/webservice/user/client/此文件夹,里面生成了你需要的客户端调用类UserService,UserService_Service。
5,调用方式如下:

UserService_Service userService=new UserService_Service();
UserService user=userService.getUserServicePort();
System.out.println(user.saveUser("afei2","yes ido"));



[color=red]注意的地方:[/color]
一是在发布的类(userServiceImpl)中假如带有接口属性,并且有其getter/setter,那么发布会失败。
二是发布的ip假如是127.0.0.1的话localhost是不能访问的,是多少就是多少,不过实际项目也不会这样的。
三是有的客户端调用的时候也会报错,比如Provider com.sun.xml.ws.spi.ProviderImpl not found这个异常,我跟踪代码进去,发觉报错的那个jdk版本的Provider和我成功的jdk版本是不一样的,在网上搜无答案,直说缺包,我感觉是个小bug。其他的没什么问题。
四是因为这里没用任何webservice框架,没有把它嵌入tomcat(假如你需要在tomcat环境中),所以端口另开,不能是8080。
[color=red]
权当记录用
[/color]

By 阿飞哥 转载请说明
腾讯微博:[url]http://t.qq.com/duyunfeiRoom[/url]
新浪微博:[url]http://weibo.com/u/1766094735[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值