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.
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.
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.
Alexey Matveev
software developer
MagicButtonLabs
Philippines
1,574 accepted answers
2 comments