Missed Team ’24? Catch up on announcements here.

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

How to use Spring MVC controllers in Stash plugin?

Remo Siegwart
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

I'm wondering whether it's possible to use Spring MVC controllers in a Stash plugin? In the source code of Stash I saw that Atlassian is using controllers to manage projects and repositories, so I tried the following:

  • Added Spring configuration file spring-mvc-servlet.xml into resources/META-INF/spring/:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.example.stash.plugins.example.controllers" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>
  • Added example controller:
package com.example.stash.plugins.example.controllers;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    public ExampleController() {
    }

    @RequestMapping(method = GET)
    public String getExamples() {
        return new String("get examples...");
    }
}

But it doesn't seem to work when trying to access http://path.to.stash/example. Is there any way to use controllers in Stash plugins?

Any help would be appreciated!

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
jhinch _Atlassian_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 20, 2013

It is possible to use spring mvc from a plugin although you can't use the version which comes with Stash, you need to bundle your own version in the plugin (i.e. make it compile scope instead of provided scope in your pom). This is largely historical due to the Atlassian plugin framework depending on spring 2.5.6 where as Stash core is on 3.2.x.

You'll also need to set up your own dispatcher servlet using a servlet module. This won't allow you to bind to http://path.to.stash/example but rather to http://path.to.stash/plugins/servlet/my-servley-module-path/example.

An example of this can be found at:

https://bitbucket.org/activeobjects/ao-plugin/src/d9a561b963d2ae93252996b95d76400086cc934d/activeobjects-plugin/src/main/resources/atlassian-plugin.xml?at=master#cl-17

All of this is fairly involved and often not worth the effort for a plugin. Currently all the bundled plugins which Stash ships (which are written by the Stash team) just use servlets directly. We known this is painful, we feel it ourselves developing plugins. We are actively looking at ways to reduce this boilerplate and hope we can provide something nicer in the near future.

Remo Siegwart
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

Awesome, exactly the example I was looking for! I'll probably give it a try and otherwise use servlets directly for my CRUD operations, as explained in this tutorial.

Thank you so much for your help!

Saurabh Gupta March 9, 2018

Hey, thanks for the sample and this answer. I wanted to implement external module into my plugin which requires spring MVC. As I am a newbie in spring and spring MVC area, didn't know what to do. This is my next pet project.

TAGS
AUG Leaders

Atlassian Community Events