java注解webservice学习第一篇

今天开始使用java1.6的注解构建一个webservice,完成创建,调用一套;好了废话少说,开始码代码。
1.创建一个webservice与部署

package cn.thinknet.server.core.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import cn.thinknet.utils.log.AKLoggerUtil;
import cn.thinknet.utils.webservice.DeployWebService;

@WebService(serviceName = "TnUserInterface", targetNamespace = "http://think-net.cn/TnUserInterface")
public class UserInterface
{

public static final int ADD = 1;

public static final int DEL = 2;

public static final int UPD = 3;

public static final String SYNC_SUCCESS = "ok";

public static final String SYNC_FAILED = "NO";

@WebMethod(action = "SyncUser")
public String syncUser(@WebParam(name = "action")
int action, @WebParam(name = "data")
String data)
{
boolean syncFlag = true;
switch (action)
{
case ADD:
syncFlag = addUser(data);
break;
case DEL:
syncFlag = delUser(data);
break;
case UPD:
syncFlag = updateUser(data);
break;
default:
AKLoggerUtil.getInstance().writeInfoLog(
"[Sync User]Don't know what you're doing.");
break;
}
return syncFlag ? SYNC_SUCCESS : SYNC_FAILED;
}

/**
* 添加用户
* @param data 用户信息
*
* @return boolean 是否同步成功
*/
private boolean addUser(String data)
{
boolean flag = true;

if(flag)
{
AKLoggerUtil.getInstance().writeInfoLog("[User]sync add user success.");
}
return flag;
}

/**
* 添加删除用户
* @param data 用户id
*
* @return boolean 是否同步成功
*/
private boolean delUser(String data)
{
boolean flag = true;

if(flag)
{
AKLoggerUtil.getInstance().writeInfoLog("[User]sync delete user success.");
}
return flag;
}

/**
* 更新用户
* @param data 用户信息Json对象
*
* @return boolean 是否同步成功
*/
private boolean updateUser(String data)
{
boolean flag = true;

if(flag)
{
AKLoggerUtil.getInstance().writeInfoLog("[User]sync update user success.");
}
return flag;
}

public static void main(String[] args)
{
// 部署webservice
DeployWebService.deployWebService("127.0.0.1", 11023, "/thinknet/TnUserInterface", new UserInterface());
}
}

2.部署工具类

package cn.thinknet.utils.webservice;

import javax.xml.ws.Endpoint;

import cn.thinknet.utils.log.AKLoggerUtil;

public class DeployWebService
{
public static final String Http_Protocol = "http";

public static void deployWebService(String hostName, int port,
String serviceName, Object obj)
{
AKLoggerUtil.getInstance().writeInfoLog(
"Deploy webservice,service class name:"
+ obj.getClass().getName());

String address = Http_Protocol + "://" + hostName + ':'
+ String.valueOf(port) + serviceName;

Endpoint.publish(address, obj);
}
}

3.创建部署完成,下面开始调用,可以先用浏览器查看生成的wsdl报文结构,在浏览器输入这个地址:【http://127.0.0.1:11023/thinknet/TnUserInterface?wsdl】,查看结果如下:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://think-net.cn/TnUserInterface" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TnUserInterface" targetNamespace="http://think-net.cn/TnUserInterface">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://think-net.cn/TnUserInterface" elementFormDefault="unqualified" targetNamespace="http://think-net.cn/TnUserInterface" version="1.0">

<xs:element name="syncUser" type="tns:syncUser"/>

<xs:element name="syncUserResponse" type="tns:syncUserResponse"/>

<xs:complexType name="syncUser">
<xs:sequence>
<xs:element name="action" type="xs:int"/>
<xs:element minOccurs="0" name="data" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="syncUserResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:schema>
</wsdl:types>
<wsdl:message name="syncUser">
<wsdl:part element="tns:syncUser" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="syncUserResponse">
<wsdl:part element="tns:syncUserResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="UserInterface">
<wsdl:operation name="syncUser">
<wsdl:input message="tns:syncUser" name="syncUser">
</wsdl:input>
<wsdl:output message="tns:syncUserResponse" name="syncUserResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TnUserInterfaceSoapBinding" type="tns:UserInterface">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="syncUser">
<soap:operation soapAction="SyncUser" style="document"/>
<wsdl:input name="syncUser">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="syncUserResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TnUserInterface">
<wsdl:port binding="tns:TnUserInterfaceSoapBinding" name="UserInterfacePort">
<soap:address location="http://127.0.0.1:11023/thinknet/TnUserInterface"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

4.采用CXF包调用,CXF调用webservice所需要的jar,请在附件中自行下载,代码如下:

public static void main(String[] args) throws Exception
{
org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory dcf = org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory
.newInstance();

org.apache.cxf.endpoint.Client client = dcf
.createClient("http://127.0.0.1:11023/thinknet/TnUserInterface?wsdl");
// sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
Object[] objects = client.invoke("syncUser", 1, "test");
// 输出调用结果
System.out.println("==================>" + objects[0].toString());
}

5.这里列出需要的jar名称,方便亲们定位:
[list]
[*]cxf-2.6.13.jar
[*]jetty-continuation-7.5.4.v20111024.jar
[*]jetty-http-7.5.4.v20111024.jar
[*]jetty-io-7.5.4.v20111024.jar
[*]jetty-server-7.5.4.v20111024.jar
[*]jetty-util-7.5.4.v20111024.jar
[*]neethi-3.0.3.jar
[*]stax2-api-3.1.1.jar
[*]woodstox-core-asl-4.2.0.jar
[*]wsdl4j-1.6.3.jar
[*]ezmorph-1.0.4.jar
[*]xmlschema-core-2.0.3.jar
[/list]
6.下面使用SOAPUI专业软件测试一下我们这个webservice吧!请看下图:
[img]http://dl2.iteye.com/upload/attachment/0096/2473/4fa9fabf-1d7a-32f2-b073-976748e645ce.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值