# prism-java **Repository Path**: trendata/prism-java ## Basic Information - **Project Name**: prism-java - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-06-06 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README shopex Prism sdk (java version) =============================================== 用途 ----------------------------------------------- 实现shopex Prism 的Java版SDK供第三方使用 功能 ----------------------------------------------- - 提供http API调用(GET/POST方式) - 连接Websocket,可以发布/消费/应答消息 - 提供oauth认证 要求 ----------------------------------------------- JDK6或者更高版本 构建安装 ----------------------------------------------- ### 使用maven构建 - 下载安装websocket客户端依赖 ```shell git clone https://github.com/hashio/websocket-client.git cd websocket-client mvn clean install ``` - 下载安装prism-java sdk ```shell git clone https://github.com/ShopEx/prism-java.git cd prism-java mvn clean install ``` - 在你的应用中添加prism-java sdk依赖到pom.xml ```xml cn.shopex prism-sdk 1.0 ``` 用法 -------------------------------------------------- - 创建PrismClient实例对象 ```java String url = "http://dilbmtcv.apihub.cn/api"; String key = "buwb2lii"; String secret = "ucr72ygfutspqeuu6s36"; Boolean https_model = true;#可选 PrismClient prismClient = new PrismClient(url,key,secret); 或者 PrismClient prismClient = new PrismClient(url,key,secret,https_model); ``` - 发起API POST请求 ```java //api请求PATH路径 String methodPath = "/platform/notify/write"; Map appParams = new HashMap(); //添加API请求所需的应用级参数 appParams.put("data","hello world"); //发送POST请求 String apiResult = prismClient.doPost(methodPath, appParams); System.out.println(apiResult); ``` - 发起API GET请求 ```java //api请求PATH路径 String methodPath = "/platform/notify/status"; Map appParams = new HashMap(); //发送GET请求 String apiResult = prismClient.doGet(methodPath,appParams); System.out.println(apiResult); ``` - websocket连接 ### 设置WebSocket生命周期函数 ```java prismClient.setPrismMsgHandler(new PrismMsgHandler() { //Websocket连接完成时触发调用 @Override public void onOpen(WebSocket socket) { System.out.println("---> open"); } //接受到Websocket服务端信息时触发调用 @Override public void onMessage(WebSocket socket, PrismMsg prismMsg) { System.out.println("---> receive msg:"+prismMsg); if (prismMsg.getTag() == 1) {//这里只对第一条消息做ACK应答 try { socket.send(prismClient.assembleAckData(prismMsg.getTag())); System.out.println("发送ACK完成:"+prismMsg.getTag()); } catch (WebSocketException e) { e.printStackTrace(); } } } //Websocket发生异常时触发调用 @Override public void onError(WebSocket socket, WebSocketException e) { e.printStackTrace(); System.out.println("---> error:"+e); } //Websocket连接关闭时触发调用 @Override public void onClose(WebSocket socket) { System.out.println("---> close"); } }); ``` ### 建立websocket连接 ```java prismClient.executeNotify(); ``` ### 发布消息 ```java prismClient.publish("order.new","hello world"); ``` ### 开启队列消费 ```java prismClient.consume(); ``` ### 应答消息 ```java prismClient.ack(1); ``` **详细使用请代码点击[这里](https://github.com/ShopEx/prism-java/blob/master/src/test/java/cn/shopex/prism/sdk/PrismClientTest.java)** - oauth认证 ```java prismClient.requireOauth(request,response);//启动oauth认证 ``` 我们以spring mvc为框架的web项目为例,下面的代码在用户登录时会启动oauth认证,SDK会检查session中是否存在session_id,如果不存在则跳转到oauth页面, 完成登录后会回跳回来。 完成登录后会将当前登录信息session_id设置到session中 ```java @Controller @RequestMapping("/user") public class LoginController { @Resource private PrismClient prismClient; //测试oauth授权 @RequestMapping("/login") public ModelAndView login(HttpServletRequest request,HttpServletResponse response) { ModelAndView view = new ModelAndView("/index"); prismClient.requireOauth(request,response); return view; } } ``` 如果授权过期,可以调用```refreshToken()```方法来刷新授权令牌 **详细代码可以在源码测试下查看,先进入[Main](https://github.com/ShopEx/prism-java/blob/master/src/test/java/cn/shopex/prism/sdk/springmvc/main/ConsoleMain.java)启动web容器,然后浏览器打开登录页面localhost:8002/user/login 即可测试Oauth认证**