[Development] In JIRA Plugin, how to use REST API in velocity template through JSON.

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2013

Hi,

In JIRA plugin development, i plan to create REST API by follow below link and create such "MyRestResource" class which will have SAVE and retrival methods.

https://developer.atlassian.com/display/DOCS/Developing+a+REST+Service+Plugin#DevelopingaRESTServicePlugin-Step6.Adjustthetestcode

Now, I need to use this REST API's save/retrieval methods into velocity template (Webwork module - JiraWebActionSupport) using JSON.

Can you please let me know how it could be achievable for calling REST API from vm template file (webwork module) in Jira Plugin and REST API will be also in same plugin project.

Thank You

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2013

I have called REST requests from velocity files before, what I did was use jQuery's ajax function.

For example:

<script type="text/javascript">
	
	#set($D = '$')
	
	AJS.${D}.ajax({
	  url: "/rest/auth/1/session",
	  type: "get",
	  dataType: "json",
	  context: document.body
	}).done(function(data) {
	  alert(data.name);
	});

</script>

Note the #set part in the code. Since $ is a keyword for velocity, this is my way of escaping it.

This code uses JIRA's REST api and i'm quite sure, you can call your own functions like that as well.

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2013

here, in my case url shows not found.

i have created rest resource class in my JIRA Plugin at location - "[FOLDER]/Src/Main/JAVA/com.company.jira.plugin.REST/PlanIssuesRestResources"

when i try to access below url then ,

http://localhost:2990/jira/rest/auth/1/PlanIssuesRestResource

shows me as below in browser:

<status>
<status-code>404</status-code>
<message>
null for uri: http://localhost:2990/jira/rest/auth/1/PlanIssuesRestResource
</message>
</status>

Can you please correct me here ?
Below is how REST Resource class created :
@Path("/message")
public class PlanIssuesRestResource {
 @GET
	    @AnonymousAllowed
	    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	    @Path("/{key}")
	   public Response getMessageFromPath(@PathParam("key") String key)
	    {
	        return Response.ok(new PlanIssuesResourceModel(key, getMessageFromKey(key))).build();
	    }

Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2013

Your path is incorrect. According to the tutorial, the path is defined like this:

http://myhost.com:port/myapp/rest/api-name/api-version/resource-name

So for you, it should be something like this:

http://localhost:2990/jira/rest/<api-name>/latest/message/<key>

<api-name> should be taken from atlassian-plugin.xml file, for example, if you have:

<rest key="helloWorldRest" path="/helloworld" version="1.0">
<description>Provides hello world services.</description>
</rest>

then it would be helloworld.

<key> part is whatever you have defined there in the function.

Have a look here for more details: https://developer.atlassian.com/display/DOCS/REST+Plugin+Module

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2013

Thank You, by follow that link URL composition, It works for below stuff:

@GET
    @AnonymousAllowed
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getMessage()
    {       return Response.ok(new PlanIssuesRestResourceModel("Hello World")).build();
    }

i can access through

host:2990/jira/rest/planissuesrestresource/1.0/message

Now, i have updated the stuff as below:

@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("/{key}")
public Response getMessageFromPath(@PathParam("key") String key)
{
   return Response.ok(new PlanIssuesRestResourceModel(key, getMessageFromKey(key))).build();
}
public Response getMessage(@QueryParam("key") String key)
{
   if(key!=null)
      return Response.ok(new PlanIssuesRestResourceModel(key, getMessageFromKey(key))).build();
   else
      return Response.ok(new PlanIssuesRestResourceModel("default","Hello World")).build();
}

and tried to access as below but, it shows me "Internal Server error" and not worked.

http://localhost:2990/jira/rest/planissuesrestresource/1.0/message/keyVal

http://localhost:2990/jira/rest/planissuesrestresource/1.0/message/key/value

How it could be accessible for query string/query path ?

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2013

It works now,

http://localhost:2990/jira/rest/planissuesrestresource/1.0/message/keyvalue

i had removed default constructore from resourceModel class so.

Thank You for your comments.

TAGS
AUG Leaders

Atlassian Community Events