Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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.

0 comments

Comment

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

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

249 views 2 1
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