注:阅读此篇时,请确保你的开发环境已经正常配置,可以正常使用命令行工具创建插件demo
简介
插件开发过程中可能会需要自定义一些rest接口方便功能设置、前端js调用等,本篇描述增加基本的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>
创建rest接口类
Confluence支持 jas-rs(Java API for RESTful Services)规范定义rest服务接口
首先需要定义一个RestApiDemo类,在此类中定义get方法用于简单返回成功的接口
这里我们定义一个ResultMsg对象:
public class ResultMsg<T> {
private Integer code = 0;
private String msg = "SUCCESS";
private T data;
// 省略get set toString方法
}
引入使用TransactionTemplate对象保证我们执行的事务性,并且使用UserManager获取用户信息,判断是否登录,当然我们也可以允许任何人都可以匿名访问接口,可以在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>
启动&验证
在终端中进入到插件项目根目录执行 atlas-run 待程序启动后admin/admin管理员默认账号密码进入,访问插件安装页http://localhost:1990/confluence/plugins/servlet/upm 选择所有插件,在列表中需要确定你的插件正常启动状态
使用Postman或者curl命令调用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