Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Create issue in jira using java program

Vineet Sharma January 4, 2019

I want to create issue in existing project in jira using java program. please suggest me how to do.

 

1 answer

1 accepted

0 votes
Answer accepted
Maarten Cautreels
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2019

Hi Vineet,

You should have a look at the following Developer Resource: Java APIs. Or have a look at an older question which could give you something to start: Create issue via REST API useing Java

Hope this helps.

Best,

Maarten

Vineet Sharma January 4, 2019

In the above link attached program i am getting 401 error

Maarten Cautreels
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2019

Hi Vineet,

401 means something is not right with the authentication. Have a look at the following line of code and make sure to adjust it to your credentials:

conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vzverev:Gromozavr".getBytes(), 0)); 

Best,

Maarten

Vineet Sharma January 4, 2019

inputstream variable is not used

package NewPost;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import java.io.*;
import java.net.*;
import javax.json.*;

public class testREST_CreateIssue {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);

HttpURLConnection conn = (HttpURLConnection) jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

System.out.println(getJSON_Body() + "/" + encodedData);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());

try {
InputStream inputStream = conn.getInputStream();
}
catch (IOException e){
System.out.println(e.getMessage());
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10105"))
).build();

return createIssue.toString();

}
}

 

Vineet Sharma January 4, 2019

if i remove inputstream then it gives error at 

conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));

Vineet Sharma January 7, 2019

Still getting error 400 in the code.

 

import java.io.*;
import java.net.*;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.json.*;

 

public class testREST_POST_CreateIssues {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");

/* Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass.toCharArray());
}
});
*/
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);

HttpURLConnection conn = (HttpURLConnection)jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

System.out.println(getJSON_Body() + "/" + encodedData);
System.out.println(getJSON_Body());
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("username:password".getBytes(),0));
conn.setRequestProperty("Content-Type", "application/json");
System.out.println(conn);
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());

try {
InputStream inputStream = conn.getInputStream();
System.out.println(inputStream);
}
catch (IOException e){
System.out.println(e.getMessage());
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10015"))
).build();

return createIssue.toString();

}
}

Suggest an answer

Log in or Sign up to answer