bboss将一个组件同时发布为webservice,hessian,http三种服务方法介绍

简介: bboss将一个组件同时发布为webservice,hessian,http三种服务方法介绍。 1.概述 bboss提供cxf webservice(基于cxf 2.7.6),hessian(基于4.0.7),http(基于bboss mvc)三种服务的一次性便捷发布机制。
bboss将一个组件同时发布为webservice,hessian,http三种服务方法介绍。

1.概述
bboss提供cxf webservice(基于cxf 2.7.6),hessian(基于4.0.7),http(基于bboss mvc)三种服务的一次性便捷发布机制。

2.服务定义
在bboss ioc配置文件中将组件同时配置为webservice、hessian、http三种服务:
<properties>
	<!-- 生成令牌控制配置文件
	author:biaoping.yin
    CopyRight:bboss
    Date:2011.4.13
	-->
	<property name="/token/*.freepage" ws:servicePort="tokenService" 
	hessian:servicePort="tokenService"
	class="com.demo.common.action.TokenController" />
</properties>

从配置中可以知道:
com.demo.common.action.TokenController是服务组件实现类,首先通过name="/token/*.freepage" 将其配置为一个mvc控制器,以便接受http请求方式的服务调用,通过ws:servicePort="tokenService" 将其发布为一个cxf webservice服务,通过hessian:servicePort="tokenService"将其发布为一个hessian服务。hessian:servicePort是可选的配置,bboss 默认将所有的组件都发布为hessian服务,默认采用name属性作为hessian服务的唯一标识,无需额外配置,这里指定hessian:servicePort是由于name属性对应的值/token/*.freepage作为mvc请求的地址匹配模式,也可以作为hessian服务的标识,但是作为hessian服务的位置地址标识不太友好,所以通过hessian:servicePort指定了hessian的标识为tokenService。
3.服务接口定义
为了让TokenController正确地发布为webservice和hessian服务,还必须定义一个接口TokenService:
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "TokenService", targetNamespace = "com.demo.common.action.TokenService")
public interface TokenService {
	public @WebResult(name = "authTempToken", partName = "partAuthTempToken")
	String genAuthTempToken(
			@WebParam(name = "appid", partName = "partAppid") String appid,
			@WebParam(name = "secret", partName = "partSecret") String secret,
			@WebParam(name = "account", partName = "partAccount") String account)
			throws Exception;

	public @WebResult(name = "dualToken", partName = "partDualToken")
	String genDualToken(
			@WebParam(name = "appid", partName = "partAppid") String appid,
			@WebParam(name = "secret", partName = "partSecret") String secret,
			@WebParam(name = "account", partName = "partAccount") String account)
			throws Exception;

	public @WebResult(name = "publicKey", partName = "partPublicKey")
	String getPublicKey(
			@WebParam(name = "appid", partName = "partAppid") String appid,
			@WebParam(name = "secret", partName = "partSecret") String secret)
			throws Exception;
	public @WebResult(name = "tempToken", partName = "partTempToken") String genTempToken() throws Exception;
}


接口方法和接口上添加了webservice服务相关的注解,要实现接口的服务组件:
@WebService(name="TokenService",targetNamespace="com.demo.common.action.TokenService")
public class TokenController implements TokenService {
	/**
	 * 获取令牌请求
	 * @param request
	 * @return
	 */
	public @ResponseBody String getToken(HttpServletRequest request)
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			return  memTokenManager.buildDToken(request);
		}
		else
		{
			return null;
		}
	}
	
	public @ResponseBody String genTempToken() throws Exception
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			return  memTokenManager.genTempToken();
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 获取令牌请求
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	public @ResponseBody String genAuthTempToken(String appid,String secret,String account) throws Exception
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			return  memTokenManager.genAuthTempToken(appid, secret, account);
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 获取令牌请求
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	public @ResponseBody String genDualToken(String appid,String secret,String account) throws Exception
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			long dualtime = 30l*24l*60l*60l*1000l;
			return  memTokenManager.genDualToken(appid, secret, account,dualtime);
		}
		else
		{
			return null;
		}
	}
	/**
	 * 获取应用公钥
	 * @param appid
	 * @param secret
	 * @return
	 * @throws Exception 
	 */
	public @ResponseBody String getPublicKey(String appid,String secret) throws Exception
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			return  memTokenManager.getPublicKey(appid, secret);
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 获取令牌请求
	 * http://localhost:8081/demo/token/getParameterToken.freepage
	 * @param request
	 * @return
	 */
	public @ResponseBody String getParameterToken(HttpServletRequest request)
	{
		MemTokenManager memTokenManager = org.frameworkset.web.token.MemTokenManagerFactory.getMemTokenManagerNoexception();
		if(memTokenManager != null)//如果开启令牌机制就会存在memTokenManager对象,否则不存在
		{
			return  memTokenManager.buildParameterDToken(request);
		}
		else
		{
			return null;
		}
	}

服务方法返回参数前都有@ResponseBody 注解,这个是用来标注mvc框架将返回的String数据作为响应返回到客户端。

4.服务的三种客户端调用方法
定义好调用参数
String appid = "appid";
String secret = "xxxxxxxxxxxxxxxxxxxxxx";
String account = "yinbp";



hessian服务方式申请token(hessian提供的标准调用模式)
HessianProxyFactory factory = new HessianProxyFactory();
//String url = "http://10.25.192.142:8081/context/hessian?service=tokenService";
String url = "http://192.168.1.101:8080"+request.getContextPath()+"/hessian?service=tokenService";
TokenService tokenService = (TokenService) factory.create(TokenService.class, url);
String token = tokenService.genAuthTempToken(appid, secret, account);
//token = tokenService.genDualToken(appid, secret, account);


webservice方式申请token(cxf提供的服务调用模式)

url = "http://192.168.1.101:8080/demo/cxfservices/tokenService";
JaxWsProxyFactoryBean WSServiceClientFactory = new  JaxWsProxyFactoryBean();
WSServiceClientFactory.setAddress(url);
WSServiceClientFactory.setServiceClass(TokenService.class);
tokenService = (TokenService)WSServiceClientFactory.create();
token = tokenService.genAuthTempToken(appid, secret, account);
//token = tokenService.genDualToken(appid, secret, account);

http请求方式申请令牌
url = "http://192.168.1.101:8080/demo/token/genAuthTempToken.freepage?appid="+appid + "&secret="+secret + "&account="+account;
//url = "http://10.25.192.142:8081/demo/token/genDualToken.freepage?appid="+appid + "&secret="+secret + "&account="+account;
token = org.frameworkset.spi.remote.http.HttpReqeust.httpPostforString(url);


5.HttpReqeust组件介绍
HttpReqeust是bboss最新版本3.8.0中提供的http服务组件,专门用来执行http请求,并将处理结果返回给客户端,提供了以下主要方法:
//发送请求,参数url对应服务地址,返回String类型的响应
public static String httpPostforString(String url) throws Exception
//发送请求,参数url对应服务地址,params中存放了请求的参数对,返回String类型的响应
public static String httpPostforString(String url,Map<String, Object> params) throws Exception
//发送请求,参数url对应服务地址,参数params中存放了请求的参数对,参数files中存放了要上传到服务端的文件信息,返回String类型的响应
public static String httpPostforString(String url,Map<String, Object> params,
			Map<String, File> files) throws Exception
目录
相关文章
|
1月前
|
Web App开发 监控 Java
|
3月前
|
网络协议 网络架构
HTTP方法有哪些?
HTTP方法有哪些?
|
3月前
|
存储 iOS开发 开发者
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
47 0
|
14天前
|
域名解析 网络协议 应用服务中间件
阿里云服务器配置免费https服务
阿里云服务器配置免费https服务
|
1月前
|
XML 开发框架 .NET
C# .NET面试系列八:ADO.NET、XML、HTTP、AJAX、WebService
## 第二部分:ADO.NET、XML、HTTP、AJAX、WebService #### 1. .NET 和 C# 有什么区别? .NET(通用语言运行时): ```c# 定义:.NET 是一个软件开发框架,提供了一个通用的运行时环境,用于在不同的编程语言中执行代码。 作用:它为多语言支持提供了一个统一的平台,允许不同的语言共享类库和其他资源。.NET 包括 Common Language Runtime (CLR)、基础类库(BCL)和其他工具。 ``` C#(C Sharp): ```c# 定义: C# 是一种由微软设计的面向对象的编程语言,专门为.NET 平台开发而创建。 作
174 2
|
1月前
|
Windows
node搭建本地https和wss服务
node搭建本地https和wss服务
25 0
|
2月前
|
XML 自然语言处理 前端开发
NLP自学习平台提供了API接口调用服务,这些接口可以通过HTTP GET请求进行调用
【2月更文挑战第7天】NLP自学习平台提供了API接口调用服务,这些接口可以通过HTTP GET请求进行调用
18 2
|
2月前
|
XML JSON 中间件
快速入门Gin框架搭建HTTP服务
快速入门Gin框架搭建HTTP服务
20 0
|
2月前
|
JSON Go 数据格式
一文搞懂Go快速搭建HTTP服务
一文搞懂Go快速搭建HTTP服务
24 0