问题
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:
- to work with json http://www.java2s.com/Code/Jar/j/Downloadjsonsimple111jar.htm
- to encode with base64: http://commons.apache.org/proper/commons-codec/download_codec.cgi
Samples to work with requests:
- https://www.baeldung.com/java-http-request
- 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