Accessing REST service of own plugin

Sascha Bobrowski March 20, 2013

Hello together,

again I'm really lost in jira plugin development. I tried to develop a rest service as written in the official docu. Here is my atlassian-plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">

[...]
  
  <rest name="My Rest Resource" i18n-name-key="my-rest-resource.name" key="my-rest-resource" path="/myrestresource" version="1.0">
    <description key="my-rest-resource.description">The My Rest Resource Plugin</description>
  </rest>
  
</atlassian-plugin>

My Java Class looks like this:

package de.rk.tools.jira.plugins.rest;

import com.atlassian.plugins.rest.common.security.AnonymousAllowed;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * A resource of message.
  */
@Path("/message")
public class MyRestResource {

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

I really don't know how to access this resource over an url. I've tried http://localhost:5990/refapp/rest/myrestresource/latest/message but only get an error page. I don't know if its important, but here my "deployment" process:

1. Run local jira with atlas-run-standalone --product jira

2. Build plugin jar with atlas-mvn clean install

3. Upload plugin into local jira.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Timothy
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.
March 20, 2013

You're missing something in your REST url:

http://localhost:5990/refapp/rest/1.0/myrestresource/latest/message

Sascha Bobrowski March 20, 2013

Both URLs doesn' work at me. But my .pom seems to be incorrect. Here is the end of my project pom:

<properties>
        <jira.version>5.1.8</jira.version>
        <amps.version>4.1.5</amps.version>
        <plugin.testrunner.version>1.1</plugin.testrunner.version>
    </properties>

The end of the POM of the demoproject in the documentation for creating a rest resource looks like this:

<properties>
        <refapp.version>2.20.0-m2</refapp.version>
        <amps.version>4.1.5</amps.version>
        <plugin.testrunner.version>1.1</plugin.testrunner.version>
    </properties>

Which is correct? The only goal I wan't to achieve in my project is to create some Java objects and access these in the gadget.xml. How I can achieve this is not important for me. With my little knowledge building a REST resource and accessing it over AJAX seems to be a good way.

My "normal" working business is to write Java beans for this issues and register them using the spring framework ;). Developing a jira plugin is very difficult for me.

Timothy
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.
March 20, 2013

Sorry, my bad. The URL should be:

http://localhost:5990/refapp/rest/myrestresource/1.0/latest/message

Can you also confirm that the REST resource is enabled in the UPM?

Sascha Bobrowski March 20, 2013

No url is working :(. Is there a way to check which URLs are available? It seems to me, that I've forgotten something. Are there some other requirements to use own rest resources?

Timothy
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.
March 20, 2013

What error are you getting?

Can you also confirm that the REST resource is enabled in the UPM?

Sascha Bobrowski March 20, 2013

It's a little bit frustrating. This URL works for me http://localhost:2990/jira/rest/myrestresource/1.0/message, but only once (don't mind about the other port). Problem is: I run "atlas-mvn (clean) install" and getting a jar, with my changes. I deploy it in my plugin manager, and all looks fine. All modules are active and the "My Rest Resource" and "Rest Servlet filter" is displayed as well. But if I reload the URL above i get a 404 again! Let me have a look in the log...

Hmm, seems interesting:

Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
	The import com.atlassian.plugins.rest cannot be resolved
	AnonymousAllowed cannot be resolved to a type

	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
	... 173 more
Caused by: java.lang.Error: Unresolved compilation problems: 
	The import com.atlassian.plugins.rest cannot be resolved
	AnonymousAllowed cannot be resolved to a type

Timothy
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.
March 20, 2013

From your latest error, you need to make sure that your plugin has all the available dependencies. The development and real version may have different dependencies set.

Sascha Bobrowski March 20, 2013

Seems, you are right. Calling multiple times "atlas-mvn clean", "atlas-mvn eclipse:clean" and "atlas-mvn eclipse:eclipse" solved the problem. Hope I can start work now. Thank for your help. I mark my question as answered.

1 vote
Steve Kling January 21, 2016

This gave me fits for a while.  But this worked:  

  1. Try changing version to 1.0.4.
  2. Then invalidate your caches and restart your IDE and re-import (if not done automatically)
  3. Inspect your IDE's log for specific errors if any.
  4. Comment out the offending reference to ...rest.common... (AnonymousAccess perhaps?) so that your plugin compiles. 
  5. From command line: atlas-run 
  6. Inspect output for errors.  If none and app starts,  shut down and return to IDE.

Check your offending reference and comment back in. 

TAGS
AUG Leaders

Atlassian Community Events