6

I need to write a webservice client and call it from Jenkins. Below are my questions:

  • What is the best way to call a web service from Jenkins? Any default plug in available? I need to pass a XML data as input to the web service.
  • If plug in is not the option, can you please let me know what are the other ways we can achieve this (ANT+JAVA etc)?
  • If you have any sample code, that would be great.

Thanks Aravind

user1961768
  • 61
  • 1
  • 1
  • 3
  • 2
    I believe this answer may be helpful. http://stackoverflow.com/questions/8303365/how-to-call-a-web-service-from-ant-script-or-from-within-jenkins – William Jan 09 '13 at 15:40

3 Answers3

3

It would be great to know you just need to call your client as part of some complex flow, implemented as a Jenkins job, or you want to concentrate on webservice testing.

WillieT has pointed you to several simple recipes which can be used to solve some basic tasks. If you need more power, better reporting, some additional features please consider the following:

Apache JMeter (details)

JMeter can be integrated into Jenkins using Performance plugin. Report example:

enter image description here

Grinder (details)

I prefer to use this tool, but it might be to complex/heavy for you.

Grinder can be integrated into Jenkins using Grinder plugin. Report example:

enter image description here

Renat Gilmanov
  • 17,735
  • 5
  • 39
  • 56
  • Thanks Renat and Willie. My major problem is to call the web service from Jenkins, not the performance. I saw the post given by Willie. But, I need to pass a complex input XML data as input to the web service from Jenkins. Need help in forming XML data from variables and pass it as input to web service. – user1961768 Jan 09 '13 at 21:30
  • 1
    JMeter provides quite an easy way to do that anyway, but you can use pure shell solution instead. CURL allows you to send request with all required data. It will look like the following: curl -X POST -H 'Content-type: text/xml' -d "Some ${JOB_NAME} string" "http://..." Please note ${JOB_NAME} in such a way you can supply all required parameters. If you need even more complex XML generation -- create a shell script and use bash to generate XML (you can use some tricks, like templates - http://stackoverflow.com/a/14032753/1435741). – Renat Gilmanov Jan 09 '13 at 21:51
  • Thanks Renat. I appreciate your time. – user1961768 Jan 10 '13 at 16:19
0

If you develop a plugin, e.g. extends hudson.tasks.Builder, include the following in pom.xml for JAX-RS Client:

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.25.1</version>
    </dependency>

A sample JAX-RS Client:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;

public class RestClient {

    private static String BASE_URL = "http://localhost:8090/rest";
    private static String ACCESS_TOKEN = "8900***bc1";

    public static String query(String path) {

        ClientConfig config = new ClientConfig();
        Client client = ClientBuilder.newClient(config);
        WebTarget target = client.target(getBaseURI());

        // token authentication
        String result = target.path(path).request().header("Authorization", "Token " + ACCESS_TOKEN)
            .accept(MediaType.APPLICATION_JSON).get(String.class);
        return result;
    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri(BASE_URL).build();
    }
}

where http://localhost:8090/rest is the base rest url outside of Jenkins environment. Anywhere in your plugin code, you can simple call this as needed:

String rsData = RestClient.query("/project_type");

assume the full rest web service url is

http://localhost:8090/rest/project_type    

You may also use Apache HttpClient, or OkHttp

Jonathan L
  • 9,552
  • 4
  • 49
  • 38
0

enter image description hereI used 'HTTP Request' Plugin. This plugin works for REST as well as SOAP api. enter image description here Plugin imageenter image description here

Anupama
  • 391
  • 4
  • 16