开发手册
更新时间:
分别介绍服务集成的提供方和依赖方的开发流程。
开发示例介绍
以实现停车场服务模型(模型ID为:“parking”)的API(标识符为:“qrcodePayAndPush”)为例,分别介绍如何实现一个托管应用,**服务提供方如何实现声明的服务模型的服务能力,以及服务依赖方如何调用该服务**。
模型定义如下:
qrcodePayAndPush接口定义如下:
服务提供方
需要实现服务模型中的API定义,应用必须实现声明的服务模型的所有的API定义,并且实现的API 入参和出参需要完全保持一致。
示例代码:
@Slf4j
@Controller
@Configuration
@RequestMapping("/parking")
publicclassParkingController{
@Resource
privateParkingServiceparkingService;
/**
* 发送支付二维码到显示屏(出场时)
*
*@paramqrcodeRequest
*@return
*/
@PostMapping("/qrcodePayAndPush")
public@ResponseBody
BaseResponse<PushQrcodeResponse>receiveMessage(
@RequestBodyBaseRequest<PushQrcodeRequest>qrcodeRequest){
System.out.println("qrcodeRequest info: "+qrcodeRequest.toString());
PushQrcodeResponsepushQrcodeResponse=newPushQrcodeResponse();
pushQrcodeResponse.setQrcodeUrl("www.xxxx.com/qrcodePayAndPush");
BaseResponse<PushQrcodeResponse>response=newBaseResponse<>();
response.setId(qrcodeRequest.getId());
response.setData(pushQrcodeResponse);
returnresponse;
}
}
publicclassPushQrcodeRequestimplementsSerializable{
privatestaticfinallongserialVersionUID=1L;
/**
* deviceUuid
*/
privateStringcampusId;
/**
* 订单号
*/
privateString tradeNo;
/**
* 需要支付的金额
*/
privateString totalAmount;
/**
* 设备uuid
*/
privateString deviceUuid;
publicString getCampusId(){
returncampusId;
}
publicvoidsetCampusId(String campusId){
this.campusId=campusId;
}
publicString getTradeNo(){
returntradeNo;
}
public voidsetTradeNo(String tradeNo){
this.tradeNo=tradeNo;
}
publicString getTotalAmount(){
returntotalAmount;
}
public voidsetTotalAmount(String totalAmount){
this.totalAmount=totalAmount;
}
publicString getDeviceUuid(){
returndeviceUuid;
}
public voidsetDeviceUuid(String deviceUuid){
this.deviceUuid=deviceUuid;
}
@Override
publicString toString(){
finalString Buildersb=newStringBuilder("{");
sb.append("\"campusId\":\"")
.append(campusId==null?"":campusId).append('\"');
sb.append(",\"tradeNo\":\"")
.append(tradeNo==null?"":tradeNo).append('\"');
sb.append(",\"totalAmount\":")
.append(totalAmount);
sb.append(",\"deviceUuid\":\"")
.append(deviceUuid==null?"":deviceUuid).append('\"');
sb.append('}');
returnsb.toString();
}
}
public classPushQrcodeResponse implementsSerializable{
private static final longserialVersionUID=1L;
/**
* qrcodeUrl
*/
privateString qrcodeUrl;
publicString getQrcodeUrl(){
returnqrcodeUrl;
}
public voidsetQrcodeUrl(String qrcodeUrl){
this.qrcodeUrl=qrcodeUrl;
}
@Override
publicString toString(){
finalStringBuilder sb=newStringBuilder("{");
sb.append("\"qrcodeUrl\":\"")
.append(qrcodeUrl==null?"":qrcodeUrl).append('\"');
sb.append('}');
returnsb.toString();
}
}
public classBaseResponse<T> implementsSerializable{
private static final longserialVersionUID=1L;
/**
* request里的全局唯一id透传
*/
privateStringid;
/**
* code
*/
private intcode=200;
/**
* 失败时必填,错误调试信息;成功时不填
*/
privateStringmessage;
/**
* 失败时必填,用户可理解语言描述的错误信息;成功时不填
*/
privateStringlocalizedMsg;
/**
* 成功时必填,失败时选填
*/
privateT data;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicintgetCode(){
returncode;
}
publicvoidsetCode(intcode){
this.code=code;
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicStringgetLocalizedMsg(){
returnlocalizedMsg;
}
publicvoidsetLocalizedMsg(StringlocalizedMsg){
this.localizedMsg=localizedMsg;
}
publicT getData(){
returndata;
}
publicvoidsetData(Tdata){
this.data=data;
}
@Override
publicStringtoString(){
finalStringBuildersb=newStringBuilder("{");
sb.append("\"id\":\"")
.append(id==null?"":id).append('\"');
sb.append(",\"code\":")
.append(code);
sb.append(",\"message\":\"")
.append(message==null?"":message).append('\"');
sb.append(",\"localizedMsg\":\"")
.append(localizedMsg==null?"":localizedMsg).append('\"');
sb.append(",\"data\":")
.append(data);
sb.append('}');
returnsb.toString();
}
}
publicclassBaseRequest<T>implementsSerializable{
privatestaticfinallongserialVersionUID=1L;
/**
* request里的全局唯一id透传
*/
privateStringid;
/**
* 请求协议版本
*/
privateStringversion;
/**
* 失败时必填,错误调试信息;成功时不填
*/
privateMap<String,Object>request;
/**
* 失败时必填,用户可理解语言描述的错误信息;成功时不填
*/
privateT params;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetVersion(){
returnversion;
}
publicvoidsetVersion(Stringversion){
this.version=version;
}
publicMap<String,Object>getRequest(){
returnrequest;
}
publicvoidsetRequest(Map<String,Object>request){
this.request=request;
}
publicT getParams(){
returnparams;
}
publicvoidsetParams(T params){
this.params=params;
}
@Override
publicStringtoString(){
finalStringBuildersb=newStringBuilder("{");
sb.append("\"id\":\"")
.append(id==null?"":id).append('\"');
sb.append(",\"version\":\"")
.append(version==null?"":version).append('\"');
sb.append(",\"request\":")
.append(request);
sb.append(",\"params\":")
.append(params);
sb.append('}');
returnsb.toString();
}
}
服务依赖方
1. 开发包依赖
<dependency>
<groupId>com.aliyun.api.gateway</groupId>
<artifactId>sdk-core-java</artifactId>
<version>1.6.0.3</version>
</dependency>
2. 示例代码
HttpClientBuilderParamsbuilderParams=newHttpClientBuilderParams();
builderParams.setAppKey("1234567");// 请填写正确的AppKey
builderParams.setAppSecret("6726732dsfdsdsfdsfds");// 请填写正确的AppSecret
ApacheHttpClientapacheHttpClient=newApacheHttpClient(builderParams);
IoTApiRequestrequest=newIoTApiRequest();
//服务模型的API的版本,注意不是服务模型的版本。
request.setApiVer("1.0");
//如果需要登录,设置当前的会话的token
//设置参数
request.putParam("campusId","testCampusId");
request.putParam("tradeNo","tradeNoTest");
request.putParam("totalAmount","12.2");
request.putParam("deviceUuid","testUuid");
//请求参数域名、path、request
Stringhost="service-mesh.api-iot.cn-shanghai.aliyuncs.com";
Stringpath="/parking/qrcodePayAndPush";
System.out.println(JSON.toJSONString(request));
ApiRequestapiRequest=newApiRequest(HttpScheme.HTTP,host,
HttpMethod.POST_BODY,path,JSON.toJSONBytes(request));
apiRequest.setHttpConnectionMode(HttpConnectionModel.MULTIPLE_CONNECTION);
ApiResponseresponse=apacheHttpClient.sendSyncRequest(apiRequest);
System.out.println(request.getId());
System.out.println(
"response code = "+response.getCode()+" response message = "+response.getMessage()
+" response content = "+newString(response.getBody(),
"utf-8"));
文档内容是否对您有帮助?