Forums

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

Create project REST API in Spring boot application for Jira Cloud Add-on does not work

Zuber Chataiwala June 12, 2019

Hello,

I am creating a Jira Cloud Add-on on Spring Boot. In that I have an endpoint which creates a project. It gives me an error with 400 status always. Here is my code:

 

@GetMapping(value = "/createproject", produces = "application/json")
public ResponseEntity<Object> createProject(@AuthenticationPrincipal AtlassianHostUser hostUser) {
String baseURL = hostUser.getHost().getBaseUrl();
StringBuffer url = new StringBuffer(baseURL);
url.append("/rest/api/2/project");
String params = "{\"projectTemplateKey\":\"com.atlassian.jira-core-project-templates:jira-core-simplified-project-management\",\"leadAccountId \":\"557058:1edab3e4-3ef7-4a27-86a2-2a5cabcf9af4\",\"avatarId\":10402,\"notificationScheme\":10000,\"name\":\"My Test Project B\",\"permissionScheme\":10004,\"assigneeType\":\"UNASSIGNED\",\"projectTypeKey\":\"software\",\"key\":\"TPB\",\"lead\":\"zuber\"}";

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
headers.set("Content-Type", "application/json");
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
HttpEntity<String> request = null;

try {
request = new HttpEntity<>(params, headers);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResponseEntity<Object> responseEntity = atlassianHostRestClients.authenticatedAsAddon().exchange(url.toString(), HttpMethod.POST, request, Object.class);
return ResponseEntity.ok().build();
}

I get following error:


2019-06-12 19:09:44.184 ERROR 8608 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 ] with root cause

org.springframework.web.client.HttpClientErrorException: 400
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at war.ProjectController.createProject(ProjectController.java:76) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]

..........

 

I verified all the Ids that I provided in POST parameters do exist. In my app descriptor I have provided scopes as ["ADMIN"]. I also checked that a Get Project API does work in my add-on. Any ideas what am I doing wrong?

Some help would be really appreciated.

Thanks

1 answer

1 vote
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 13, 2019

Hi @Zuber Chataiwala ,

I believe the problem is the the body of the REST request you are sending. Indeed, if I try to run the same REST API call using curl (changing the data in order to make it work for my instance) I get error 400 in return as well, saying: Unrecognized field \"leadAccountId \"

~$ curl -D- -u EMAIL_ADDRESS:API_TOKEN -H "Accept: application/json" -H "Content-Type: application/json" -d '{"projectTemplateKey":"com.atlassian.jira-core-project-templates:jira-core-simplified-project-management","leadAccountId ":"557057:883024c6-xxxxxxxxxxxxxx","notificationScheme":10000,"name":"Test Create Project REST","permissionScheme":10025,"assigneeType":"UNASSIGNED","projectTypeKey":"software","key":"TCPR","lead":"USERNAME"}' -X POST https://xxxxxxxxxx.atlassian.net/rest/api/2/project

HTTP/2 400
.....
...

{"errorMessages":["Unrecognized field \"leadAccountId \" (Class com.atlassian.jira.rest.v2.issue.project.ProjectInputBean), not marked as ignorable\n at [Source: org.apache.catalina.connector.CoyoteInputStream@22b11c7; line: 1, column: 125] (through reference chain: com.atlassian.jira.rest.v2.issue.project.ProjectInputBean[\"leadAccountId \"])"]}

 

 

This is a bug that reproduces also against the REST API v3 endpoint and I will proceed opening a bug request for this.

However, before going any further, I have found some other issues with the provided request body and I'd like to ask you to test the following and confirm it works:

 

When I remove the leadAccountId I then get a different error saying:

"An invalid project template was specified. Make sure the project template matches the project type you specified."

This actually makes sense since we provided  the template for a Jira Core project and therefore we cannot set projectTypeKey to "software".

 

After setting  projectTypeKey to "business" the call is now working as expected:

$ curl -D- -u EMAIL:API-TOKEN -H "Accept: application/json" -H "Content-Type: application/json" -d '{"projectTemplateKey":"com.atlassian.jira-core-project-templates:jira-core-simplified-project-management","notificationScheme":10000,"name":"Test Create Project REST","permissionScheme":0,"assigneeType":"UNASSIGNED","projectTypeKey":"business","key":"TCPR","lead":"USERNAME"}' -X POST https://XXXXXX.atlassian.net/rest/api/2/project

HTTP/2 201
...

{"self":"https://xxxxxxx.atlassian.net/rest/api/2/project/10304","id":10304,"key":"TCPR"}

 

The body I used for the REST request is the following:

{
"projectTemplateKey": "com.atlassian.jira-core-project-templates:jira-core-simplified-project-management",
"notificationScheme": 10000,
"name": "Test Create Project REST",
"permissionScheme": 0,
"assigneeType": "UNASSIGNED",
"projectTypeKey": "business",
"key": "TCPR",
"lead": "USERNAME_HERE"
}

 

Can you kindly test above request body (replacing the values with the ones that apply to your instance) and confirm this works?

 

 

Cheers,
Dario

Zuber Chataiwala June 13, 2019

Hi @Dario B ,

Thanks for the help.  I did try your request body using curl and it worked. But it is still giving me 400 error code and same exception when I use the request body in spring boot. Following is my updated request body:

 

String params = "{\"projectTemplateKey\":\"com.atlassian.jira-core-project-templates:jira-core-simplified-project-management\""
+ ",\"notificationScheme\":10000"
+ ",\"name\":\"My Test Project B\""
+ ",\"permissionScheme\":0"
+ ",\"assigneeType\":\"UNASSIGNED\""
+ ",\"projectTypeKey\":\"business\""
+ ",\"key\":\"TPB\""
+ ",\"lead\":\"admin\""
+ "}";

The above request parameters work using curl command. Any ideas what is missing in Spring boot code?

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 14, 2019

Hi @Zuber Chataiwala , 

Looking at the provided snipped there seems to be some issues with the double quotes: some times they are unbalanced and sometimes there are double quotes where there are not supposed to be (e.g. after an integer).

Most likely you are using an editor (like e.g. sublime text) that automatically closes the double quotes when you type and you didn't notice the extra ones. Indeed, if you look at the text you pasted in the code block, some times the double quotes are rendered differently ( green or black depending if they are escaped or not).

Can you change the  body as it follows and see if it works?

String params = "{\"projectTemplateKey\":\"com.atlassian.jira-core-project-templates:jira-core-simplified-project-management\"
+ ,\"notificationScheme\":10000
+ ,\"name\":\"My Test Project B\"
+ ,\"permissionScheme\":0
+ ,\"assigneeType\":\"UNASSIGNED\"
+ ,\"projectTypeKey\":\"business\"
+ ,\"key\":\"TPB\"
+ ,\"lead\":\"admin\"
+ }";

 

 

Cheers,
Dario

Zuber Chataiwala June 14, 2019

Hi @Dario B ,

I am working on Eclipse. I did verify that the code I pasted is as is taken from the editor. Eclipse does not show me syntax error. If I remove those additional quotes, Eclipse will show my a syntax error because I need to provide quotes around '+' otherwise '+' will be taken as String literal inside the request body.

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 14, 2019

Hi @Zuber Chataiwala ,

Thanks for clarifying, having this in mind then I can confirm that the provided string is correct.

However, together with the error 400, you should get some more details on what is  failing in the response body (like we have seen using curl). Can you copy/paste the error message you get?

 

Also, FYI, I have created the below bug to report the misbehavior of the project REST API endpoints (v2 and v3):

 

Finally, please notice that if the problem is with the code itself and not with the provided values I am afraid I may not be the best person to help. You might want to ask this question on the developers' community instead:

 

 

Cheers,
Dario

Zuber Chataiwala June 14, 2019

Thanks @Dario B  for raising the bug and your suggestions. I posted a topic in developer community here : https://community.developer.atlassian.com/t/create-project-rest-api-in-spring-boot-application-for-jira-cloud-add-on-gives-400-error/29899

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 14, 2019

You are very welcome @Zuber Chataiwala :) 

Still, in case you are getting any error message I may be able to help a  bit further. In case you don't get any error message in return then the developer community should be the best place to get help.

Have a nice weekend!

 

Dario

Like Zuber Chataiwala likes this

Suggest an answer

Log in or Sign up to answer