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:
<?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>
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!
Community moderators have prevented the ability to post new answers.
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:
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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.