Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Jira Server/Data Center apps: Make REST API Browser see what your POST REST method consumes

In this article I will show you how to make REST API Browser see that your POST REST endpoint consumes application/json in Jira Server/ Data Center apps.

Problem

Let's create a new Jira Server app via Atlassian SDK and add a new REST plugin module. By default your file src/main/java/ru/matveev/alexey/atlassian/tutorial/rest/MyRestEndpoint.java will look like this:

@Path("/message")
public class MyRestEndpoint {
@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getMessage() {
return Response.ok(new MyRestEndpointModel("Hello World")).build();
}
}

We have a GET REST endpoint. Let's change it to POST:

@Path("/message")
public class MyRestEndpoint {
@POST
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response postMessage(String body) {
return Response.ok(new MyRestEndpointModel(body)).build();
}
}

I changed annotation @get to @post. Also I pass the body parameter which contains the request body and return this request body as the method result.

Now let's package our plugin and run it in Jira.

After Jira started go to REST API Browser and find your POST REST endpoint.

Screenshot 2020-09-15 at 17.38.05.png

As you can see the Representation field shows */* but we would like to have application/json.

And we can make it also another way.

I added the src/main/java/ru/matveev/alexey/atlassian/tutorial/rest/MyRequestBody.java file:

@Data
public class MyRequestBody {
String value;
}

And then I changed our POST method to this one:

@Path("/message")
public class MyRestEndpoint {

@POST
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON})
public Response postMessage(MyRequestBody body)
{
return Response.ok(new MyRestEndpointModel(body.getValue())).build();
}
}

As you can see I changed the body parameter from String to MyRequestBody, which means that we need to pass a json like this:

{"value": "Hello World"}

Now let's execute our POST method.

This image has an empty alt attribute; its file name is Screenshot-2020-09-16-at-11.27.48-1024x446.png

We have 415 error which means Unsupported Media Type. And that is correct we should choose application/json as our content type not */*. But we can not do it.

Solution

You need to add @Consumes annotation to your method:

@Path("/message")
public class MyRestEndpoint {

@POST
@AnonymousAllowed
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response postMessage(MyRequestBody body)
{
return Response.ok(new MyRestEndpointModel(body.getValue())).build();
}
}

I added this line:

@Consumes({MediaType.APPLICATION_JSON})

Now you can repackage your app and see the result.

This image has an empty alt attribute; its file name is Screenshot-2020-09-16-at-11.35.07-1024x461.png

That is what we wanted. We can choose application/json and our POST REST method works.

1 comment

Comment

Log in or Sign up to comment
M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2021

Interesting content

TAGS
AUG Leaders

Atlassian Community Events