how to connect VSTS Azure Devops using rest api in java? i am not getting any documentation in java for this [closed]

送分小仙女□ 提交于 2020-06-01 08:05:10

问题


I need to connect VSTS Server using rest API in java. i have been through the documentation provided by Microsoft, but its for c# i need sample java programme for java, is there any jar released by Microsoft for VSTS, as i cannot find any jar related to this. Using c# i am able to connect with Vsts but i want some sample code for java.

sample code i have used in c# is :

public static async void GetProjects()
{
try
{
    var personalaccesstoken = "PAT_FROM_WEBSITE";

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(
            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
            Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                    string.Format("{0}:{1}", "", personalaccesstoken))));

        using (HttpResponseMessage response = await client.GetAsync(
                    "https://dev.azure.com/{organization}/_apis/projects"))
        {
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}
}

回答1:


This is my first java experience ))

Try that to get work item:

package com.restapi.sample;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ResApiMain {

    static String ServiceUrl = "https://dev.azure.com/<your_org>/";
    static String TeamProjectName = "your_team_project_name";
    static String UrlEndGetWorkItemById = "/_apis/wit/workitems/";
    static Integer WorkItemId = 1208;
    static String PAT = "your_pat";

    public static void main(String[] args) {

        try {

            String AuthStr = ":" + PAT;
            Base64 base64 = new Base64();

            String encodedPAT = new String(base64.encode(AuthStr.getBytes()));

            URL url = new URL(ServiceUrl + TeamProjectName + UrlEndGetWorkItemById + WorkItemId.toString());
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestProperty("Authorization", "Basic " + encodedPAT);
            System.out.println("URL - " + url.toString());
            System.out.println("PAT - " + encodedPAT);
            con.setRequestMethod("GET");

            int status = con.getResponseCode();

            if (status == 200){
                String responseBody;
                try (Scanner scanner = new Scanner(con.getInputStream())) {
                    responseBody = scanner.useDelimiter("\\A").next();
                    System.out.println(responseBody);
                }

                try {
                    Object obj = new JSONParser().parse(responseBody);
                    JSONObject jo = (JSONObject) obj;

                    String WIID = (String) jo.get("id").toString();
                    Map<String, String> fields = (Map<String, String>) jo.get("fields");
                    System.out.println("WorkItemId - " + WIID);
                    System.out.println("WorkItemTitle - " + fields.get("System.Title"));
                } catch (ParseException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }           

            con.disconnect();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Additional jars:

  1. to work with json http://www.java2s.com/Code/Jar/j/Downloadjsonsimple111jar.htm
  2. to encode with base64: http://commons.apache.org/proper/commons-codec/download_codec.cgi

Samples to work with requests:

  1. https://www.baeldung.com/java-http-request
  2. How to use java.net.URLConnection to fire and handle HTTP requests

Check the url generated in the eclipse console:



来源:https://stackoverflow.com/questions/55044652/how-to-connect-vsts-azure-devops-using-rest-api-in-java-i-am-not-getting-any-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!