public interface WebService extends Definable<WebService.Context>
Webservice
the ws is fully implemented in Java and does not require any Ruby on Rails code.
The classes implementing this extension point must be declared in SonarPlugin.getExtensions().
public class HelloWs implements WebService {
@Override
public void define(Context context) {
NewController controller = context.createController("api/hello");
controller.setDescription("Web service example");
// create the URL /api/hello/show
controller.createAction("show")
.setDescription("Entry point")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
// read request parameters and generates response output
response.newJsonWriter()
.beginObject()
.prop("hello", request.mandatoryParam("key"))
.endObject()
.close();
}
})
.createParam("key").setDescription("Example key").setRequired(true);
// important to apply changes
controller.done();
}
}
public class HelloWsTest {
WebService ws = new HelloWs();
@Test
public void should_define_ws() throws Exception {
// WsTester is available in the Maven artifact org.codehaus.sonar:sonar-testing-harness
WsTester tester = new WsTester(ws);
WebService.Controller controller = tester.controller("api/hello");
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo("api/hello");
assertThat(controller.description()).isNotEmpty();
assertThat(controller.actions()).hasSize(1);
WebService.Action show = controller.action("show");
assertThat(show).isNotNull();
assertThat(show.key()).isEqualTo("show");
assertThat(index.handler()).isNotNull();
}
}
| Modifier and Type | Interface and Description |
|---|---|
static class |
WebService.Action |
static class |
WebService.Context |
static class |
WebService.Controller |
static class |
WebService.NewAction |
static class |
WebService.NewController |
static class |
WebService.NewParam |
static class |
WebService.Param |
static class |
WebService.SelectionMode |
| Modifier and Type | Method and Description |
|---|---|
void |
define(WebService.Context context)
Executed once at server startup.
|
void define(WebService.Context context)
define in interface Definable<WebService.Context>Copyright © 2009–2015 SonarSource. All rights reserved.