如何在Java中使用Rest API连接VSTS Azure Devops?我没有为此获得任何Java文档 [英] how to connect VSTS Azure Devops using rest api in java? i am not getting any documentation in java for this

查看:138
本文介绍了如何在Java中使用Rest API连接VSTS Azure Devops?我没有为此获得任何Java文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java中的rest API连接VSTS Server.我已经阅读过Microsoft提供的文档,但是对于c#,我需要Java的示例Java程序,Microsoft是否为VSTS发布了任何jar,因为我找不到与此相关的jar.使用c#我能够与Vsts连接,但是我想要一些Java的示例代码.

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.

我在C#中使用的示例代码是:

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());
}
}

推荐答案

这是我的第一个Java经验))

This is my first java experience ))

尝试获取工作项:

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();
        }

    }

}

其他罐子:

  1. 使用json http://www.java2s.com/Code/Jar/j/Downloadjsonsimple111jar.htm
  2. 使用base64进行编码: http://commons.apache.org/正确/commons-codec/download_codec.cgi
  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

处理请求的示例:

  1. https://www.baeldung.com/java-http-request
  2. 如何使用Java. net.URLConnection触发并处理HTTP请求
  1. https://www.baeldung.com/java-http-request
  2. How to use java.net.URLConnection to fire and handle HTTP requests

检查在eclipse控制台中生成的url:

Check the url generated in the eclipse console:

这篇关于如何在Java中使用Rest API连接VSTS Azure Devops?我没有为此获得任何Java文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆