Create project REST API in Spring boot application for Jira Cloud Add-on gives 400 error

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\""
+ ",\"notificationScheme\":10000"
+ ",\"name\":\"My Test Project B\""
+ ",\"permissionScheme\":0"
+ ",\"assigneeType\":\"UNASSIGNED\""
+ ",\"projectTypeKey\":\"business\""
+ ",\"key\":\"TPB\""
+ ",\"lead\":\"admin\""
+ "}";

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 exception:

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]

..........

The request body works using a curl command.

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

Hi @zuber,

I assume that Jira is returning a detailed error message along with the 400 status code, so I suggest making sure that is printed first of all.

You can bump the log level of RestTemplate using logging.level.org.springframework.web.client=DEBUG, as described in this StackOverflow post.

But I also recall coming across SPR-9367, suggesting that the default SimpleClientHttpRequestFactory doesn’t support this, and that the best solution is switching the underlying HTTP client to Apache HttpComponents.

@Bean
public RestTemplateCustomizer httpComponentsRestTemplateRequestFactoryCustomizer() {
    return restTemplate -> restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
}

Once you’ve done that, you can also bump logging.level.org.apache.http=DEBUG.

1 Like

Hi @epehrson,

Thanks for guiding how to debug the API request. I get the following error message in response:

"{"errorMessages":["'accountId' must be the only user identifying query parameter in GDPR strict mode."],"errors":{}}[\r][\n]"

I tried disabling gdpr under apiMigrations. I am still getting this error. Is there anything that I am missing?
Thanks

@zuber, see the documentation for the lead parameter to POST /rest/api/3/project.

The GDPR deprecation period has officially ended, and the request you are making needs to satisfy the new contract.

1 Like

Hi @epehrson,

Do you mean the “lead” parameter is no longer available even when I use /rest/api/2/project ? I even tried using “leadAccountId” instead of “lead” but then that gave me unrecognized field error for it:

"{"errorMessages":["Unrecognized field \"leadAccountId \" (Class com.atlassian.jira.rest.v2.issue.project.ProjectInputBean), not marked as ignorable\n at [Source: com.atlassian.plugin.connect.plugin.auth.scope.InputConsumingHttpServletRequest$1@60dc487f; line: 1, column: 125] (through reference chain: com.atlassian.jira.rest.v2.issue.project.ProjectInputBean[\"leadAccountId \"])"]}[\r][\n]"

@zuber the error message suggests you have a trailing space in the field name "leadAccountId ".

2 Likes

Oops! I missed such a small thing here. Thanks @epehrson for pointing it! It works now!

Hi @zuber
I am also trying the same rest call and getting the same errors first thing how u passed this leadAccountId value in params and second thing for resolving this 400 status code. I added the @Bean in the main class and after that in yml file debug also but still same errors. Can u share your code or thoughts about how to fix these issues.
It will be a great help. Thanks

Hi @epehrson
Can You Please tell me how to do that i have written this bean in my main class but still getiing same error.
Any suggestions what i am doing wrong.
Thanks