注:閱讀此篇時募逞,請確保你的開發(fā)環(huán)境已經(jīng)正常配置鬼譬,可以正常使用命令行工具創(chuàng)建插件demo
簡介
插件開發(fā)過程中可能會需要自定義一些rest接口方便功能設(shè)置焚鹊、前端js調(diào)用等刃宵,本篇描述增加基本的rest api
詳細說明
引入依賴
<!--必需-->
<dependency>
<groupId>com.atlassian.plugins.rest</groupId>
<artifactId>atlassian-rest-common</artifactId>
<version>6.1.2</version>
<scope>provided</scope>
</dependency>
<!--必需-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--非必需 如果需要判斷用戶是否登錄需要次api-->
<dependency>
<groupId>com.atlassian.sal</groupId>
<artifactId>sal-api</artifactId>
<version>4.3.0</version>
<scope>provided</scope>
</dependency>
創(chuàng)建rest接口類
Confluence支持 jas-rs(Java API for RESTful Services)規(guī)范定義rest服務(wù)接口
首先需要定義一個RestApiDemo類衡瓶,在此類中定義get方法用于簡單返回成功的接口
這里我們定義一個ResultMsg對象:
public class ResultMsg<T> {
private Integer code = 0;
private String msg = "SUCCESS";
private T data;
// 省略get set toString方法
}
引入使用TransactionTemplate對象保證我們執(zhí)行的事務(wù)性,并且使用UserManager獲取用戶信息牲证,判斷是否登錄哮针,當(dāng)然我們也可以允許任何人都可以匿名訪問接口,可以在get方法上增加一個@AnonymousAllowed注解坦袍。
以下為demo類完整示例:
package cn.idocode.confluence.restapi;
import cn.idocode.confluence.model.ResultMsg;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.transaction.TransactionTemplate;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.sal.api.user.UserProfile;
import com.google.gson.Gson;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/demo")
@Scanned
public class RestApiDemo {
@ComponentImport
private UserManager userManager;
@ComponentImport
private TransactionTemplate transactionTemplate;
@Inject
public RestApiDemo(UserManager userManager, TransactionTemplate transactionTemplate) {
this.userManager = userManager;
this.transactionTemplate = transactionTemplate;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response get(@Context HttpServletRequest request) {
if (!checkUserHasLogin(request)) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
return Response.ok(this.transactionTemplate.execute(
() -> {
ResultMsg resultMsg = new ResultMsg();
Gson gson = new Gson();
return gson.toJson(resultMsg);
}
)).build();
}
private boolean checkUserHasLogin(HttpServletRequest request) {
// boolean result = false;
// UserProfile userProfile = this.userManager.getRemoteUser(request);
// if (userProfile != null) {
// String userName = userProfile.getUsername();
// result = userName != null && this.userManager.isSystemAdmin(userName);
// }
// return result;
return true;
}
}
注冊rest模塊
在插件項目配置文件atlassian-plugin.xml中增加以下模塊配置
<rest name="demo-rest" key="demo-rest" path="/plugin-demo" version="1.0">
<description>
a demo rest api
</description>
</rest>
啟動&驗證
在終端中進入到插件項目根目錄執(zhí)行 atlas-run 待程序啟動后admin/admin管理員默認賬號密碼進入十厢,訪問插件安裝頁http://localhost:1990/confluence/plugins/servlet/upm 選擇所有插件,在列表中需要確定你的插件正常啟動狀態(tài)
使用Postman或者curl命令調(diào)用get接口
curl命令示例
curl -i "http://localhost:1990/confluence/rest/plugin-demo/1.0/demo"
HTTP/1.1 200
X-Content-Type-Options: nosniff
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 01 May 2020 05:36:56 GMT
{"code":0,"msg":"SUCCESS"}%
示例代碼
https://github.com/chaoyz/plugin-demo