Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Problems with my own Parameterized Rest API

Bjarne Obst June 26, 2013

Hey,

yesterday I started of writing my own little REST Service Plugins for Confluence.

For that I followed the Tutorial which originally is wrien for refapp. Everything worked fine but then I encountered an error I just can't explain. In the tutorial just a simple string was passed to the the function and to the resourceModel. I now wanted to enhance this by again passing a string. But this time I used the string as a key for the getSpace() function which then returned the specific page to me. For that I also changed the ResourceModel to the following. (I am sorry for my text in the Codeboxes)

@XmlElement(name = "key") 
	private String key;

	private MyRestResourceModel() { //default
	}

	public MyRestResourceModel(String message, Space key) { //RestResourceModel must contain a sting and a space
		this.message = message;
		this.key = key.getName();
	
	}

And the RestResource to the following:
 
	public Space getSpacePages(String spaceKey) {
		Space currentSpace = spaceManager.getSpace(spaceKey);
return currentSpace;
}


@GET
	@AnonymousAllowed
	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
	public Response getMessage(@QueryParam("key") String key) {
		if (key != null) {
			return Response.ok(
					new MyRestResourceModel("Das Ist die Message",
							getSpacePages(key))).build();
		} else {
			
			
			....
		}

	}

 Also this worked out fine. But then I tried to retrieve all the pages for one Space.
In order to achieve this I enhanced my getSpacePages function again:

	public ArrayList<String> getSpacePages(String spaceKey) {
		Space currentSpace = spaceManager.getSpace(spaceKey);
		List<Page> allPages = pageManager.getPages(currentSpace, true);
		// for (String StringPages: allPages.get().getName())
		Object Hoi[];
		Hoi = allPages.toArray();
		int i;
		for (i = 0; i <= 100000000; i++) {
			StringPages.set(i, Hoi[i].toString());
		}
		return StringPages;
	}

Changed model:
@XmlAnyElement
	private ArrayList&lt;String&gt; key;

	private MyRestResourceModel() { //default
	}

	public MyRestResourceModel(String message, ArrayList&lt;String&gt; key) { //RestResourceModel must contain a sting and a space
		this.message = message;
		this.key = key;
	
	}

	public ArrayList&lt;String&gt; getKey() { //getter and setter methods
		return key;
	}

	public void setKey(ArrayList&lt;String&gt; key) {
		this.key = key;
	}

}

However I found out that my style of converting and programming is not that nice (I have allready found a smoother solution for the above code which does not rely on this stupid for loops and this many type conversions) but still is not the real problem and therefore I would like to ask my question:

For me it is somehow impossible to pass Lists, ArrayLists or Arrays. I always end up with the <message>500</message> which basically tells me nothing. (well there was a server error but this does not help me at all)

I always paid attention to the type conversions and of course I also always adapted the Model in the same way. My Java code was free from errors but somehow the rest plugin itself or the Server can't handle my requests/output/or what ever.

I really really hope that anyone can help me although I might just be to stupid to see a simple mistake. Well, I am just a beginner so please forgive me.

In the same moment I would also like to ask for a tutorial on how to write your own Rest Plugins. (a little more detailed than the one I used)

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
Bjarne Obst July 18, 2013
@GET //get method -&gt; function retrieves data
    @AnonymousAllowed //anonymous access is allowed for this function &lt;- no authentication needed 
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) //defines which datatype is produces by the function, in this case Json
    @Path("/users") //function is accessible under the path "users" -&gt; confluence/rest/spacepagesresource/1.0/restapi/users
    public Response getMessage(@QueryParam("userkey")String userkey) throws EntityException //function + Exception
    {	
			@SuppressWarnings("deprecation")
			Pager&lt;User&gt; allUsers = userAccessor.getUsersWithConfluenceAccess();
    		List&lt;Userss&gt; allUserss = new ArrayList(); //creating new List of class Userss -&gt; see class Userss
    		for (User u: allUsers){ //for every User in allUsers do
    			if (u.getFullName().contains(userkey) || u.getName().contains(userkey) ){
        			Userss mytemp = new Userss(); //creates new object of the class Userss
    				mytemp.setName(u.getFullName()); // extracts the full name of the first user in allUsers
    				allUserss.add(mytemp); //adds the the element to the list AllUserss of type Userss
    			}
    		}
    		return Response.ok(allUserss).build(); //builds and returns allUserss 
    }

Okay, I just did it differently now. This way everything works for me.

0 votes
Bjarne Obst June 27, 2013

Okay, as I already said: I figured out a better way to handle the pages of space. Right now it is possible for me to put all the pages into a single string and return this one but still this definetely is not satisfying me:

public String getSpacePages(String spaceKey) {
		Space currentSpace = spaceManager.getSpace(spaceKey);
		List &lt;Page&gt; allPages = pageManager.getPages(currentSpace, true);
		String Stringer = allPages.toString();
		return Stringer;

	}

I am still asking myself wether it is possible to handle Lists, Arraylists or Arrays with a rest plugin. Does anyone have any knowledge concerning this issue? Because returning lists would make a lot of things a lot of easier + the facility of aspection is a lot better.

Regards

TAGS
AUG Leaders

Atlassian Community Events