Jira Server/Data Center apps: Make REST API Browser see that your POST REST consumes application/json

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.

This image has an empty alt attribute; its file name is Screenshot-2020-09-15-at-17.38.05-1-1024x451.png

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

And we can make it even worse.

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.

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.

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

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

%d bloggers like this:

Spelling error report

The following text will be sent to our editors: