Here's a small example showing how to get JSON data from the Rest API in Java, picking a scenario from the common use cases in the REST API Documentation for the sake of the demonstration. The example will retrieve user information.
According to the REST API Documentation, to get the user's data you need to use the following endpoint:
http://<TeamCity Server host>:<port>/app/rest/users
Using this url in the browser, you get a response like this:
//Get and process response InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader br = new BufferedReader(in); String output; while ((output = br.readLine()) != null) { //This is the response from the server System.out.println("This is the response from the server"); System.out.println(output);
//From here you can deserialize the JSON to your object in Java Gson gson = new Gson(); userList = gson.fromJson(output, TCUserResponse.class); }
// To output the data we deserialized to our object Iterator<TCUser> iter = userList.getUser().iterator();
System.out.println("This is the data we got after deserialization to our object"); while (iter.hasNext()) { System.out.println(iter.next().toString()); } conn.disconnect();
When you run this example, the output would be something like this:
This is the response from the server {"count":2,"user":[{"username":"USER1","name":"User1 name","id":1,"href":"/app/rest/users/id:1"},{"username":"USER2","name":"User2 name","id":2,"href":"/app/rest/users/id:2"}]} This is the data we got after deserialization to our object TCUser{username='USER1', name='User1 name', id=1, href='/app/rest/users/id:1'} TCUser{username='USER2', name='User2 name', id=2, href='/app/rest/users/id:2'}
Process finished with exit code 0
Note: You can read more about TeamCity REST authentication here.
I hope this small example is enough to get you going. Please let me know if you have any more questions.
Hello,
public class TCUserResponse {
int count;
ArrayList<TCUser> user = new ArrayList<TCUser>();
public TCUserResponse() {
}
public TCUserResponse(int count, ArrayList<TCUser> user) {
this.count = count;
this.user = user;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ArrayList<TCUser> getUser() {
return user;
}
public void setUser(ArrayList<TCUser> user) {
this.user = user;
}
}
public class TCUser {
String username;
String name;
int id;
String href;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public TCUser(String username, String name, int id, String href) {
this.username = username;
this.name = name;
this.id = id;
this.href = href;
}
@Override
public String toString() {
return "TCUser{" +
"username='" + username + '\'' +
", name='" + name + '\'' +
", id=" + id +
", href='" + href + '\'' +
'}';
}
}