我正在尝试开发 Atlassian JIRA小工具,它将使用 REST模块从 .java 文件中获取值。为了解决这个问题,我阅读了JIRA文档(适用于REST,gadgets等等)并参考了示例。所以我得到了i.a.以下代码:
gadget.xml: ...
<Content type="html" view="profile">
<![CDATA[
#requireResource("com.atlassian.jira.gadgets:common")
#includeResources()
<script type="text/javascript">
(function () {
var gadget = AJS.Gadget({
baseUrl: "__ATLASSIAN_BASE_URL__",
useOauth: "/rest/gadget/1.0/currentUser",
view: {
template: function(args) {
var gadget = this;
var userDetails = AJS.$("<h1/>").text("Hello, "+args.fred.firstName);
gadget.getView().html(userDetails);
},
args: [{
key: "fred",
ajaxOptions: function() {
return {
url: "/rest/My_Dashboard/1.0/users"
};
}
}]
}
});
})();
</script>
]]>
RestHelloWorldService.java:
package com.atlassian.jira.plugin.tutorial;
import com.atlassian.jira.plugin.tutorial.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class RestHelloWorldService {
@GET
@Path("users")
public Response getUncompletedUsers() {
return Response.ok(new User("Fred","Bloggs")).build();
}
}
User.java:
package com.atlassian.jira.plugin.tutorial;
import com.atlassian.jira.plugin.tutorial.*;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class User
{
@XmlElement
private String firstName;
@XmlElement
private String lastName;
// This private constructor isn't used by any code, but JAXB requires any
// representation class to have a no-args constructor.
private User() { }
public User(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
建立成功!
但是我的小工具仍然没有显示任何内容。数据位于URL位置:
<user>
<firstName>Fred</firstName>
<lastName>Bloggs</lastName>
</user>
可能有什么不对? 我没有想法......
答案 0 :(得分:0)
我建议您指定REST资源路径,如下例所示,MIME类型为MediaType.TEXT_HTML
。
@Path("/users")
@AnonymousAllowed
public class RestHelloWorldService {
@GET
@Produces(MediaType.TEXT_HTML)
public Response getUncompletedUsers() {
return Response.ok(new User("Fred", "Bloggs")).build();
}
}
另见:
答案 1 :(得分:0)
我必须在gadget.xml中添加两行来将contentType和dataType设置为JSON:
...args: [{
key: "fred",
ajaxOptions: function() {
return {
url: "/rest/My_Dashboard/1.0/users",
contentType: "application/json",
dataType: "json",
};
}
}]
无论如何,谢谢你的答案!