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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Mohammed Amine Community Leader Jun 04, 2021

Interesting content

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

324 views 3 14
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you