POSTing `JSON` to an `ArrayList` gets the response "Request JSON Mapping Error"

164 views Asked by At

I have a problem POSTing JSON to an ArrayList

I have a class Plan

 public class Plan {
    private String planId;
    private String planName;
        :
        :
}

and an ArrayList of Plan - PlanList

public class PlanList {
    private List<Plan> plans = new ArrayList<Plan>();
        :
        :
}

I have POST and GET REST APIs

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Plan createPlan(@CookieParam(SmsHttpHeaders.X_SMS_AUTH_TOKEN) String token, Plan plan, @HeaderParam("Organization-Id") String organizationIdByService);

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public PlanList createPlans(@CookieParam(SmsHttpHeaders.X_SMS_AUTH_TOKEN) String token, PlanList plans, @HeaderParam("Organization-Id") String organizationIdByService);

@GET
@Produces(MediaType.APPLICATION_JSON)
public PlanList retrieveAllPlans(@CookieParam(SmsHttpHeaders.X_SMS_AUTH_TOKEN) String token, @HeaderParam("Organization-Id") String organizationIdByService);

When I GET retrieveAllPlans, I get back the following JSON, just as I expect.

{
  "plans": [
    {
      "planId":"1",
      "planName":"Plan 1"
    },
    {
      "planId":"2",
      "planName":"Plan 2"
    },
    {
      "planId":"3",
      "planName":"Plan 3"
    }
  ]
}

POSTing a single Plan, createPlan, works correctly.
However, when I try to POST to createPlans in the same format that the GET returns, I get the response "Request JSON Mapping Error".

Is the JSON incorrectly formatted? Is my REST definition wrong?

2

There are 2 answers

4
Robert Moskal On BEST ANSWER

Both of your post functions are being mapped to the same http endpoint. There is probably a @Path notation on the class specifying a single endpoint for all its methods and RestEasy is trying to distinguish by http method (post, get, etc..).

You'll need to specify a unique @Path annotation for each post function. Say:

@Path("/plan")

For the first, and

@Path("/plans")

For the second.

0
Ruslan Gunawardana On

The problem is, that when you try to POST to createPlans, the request is being handled by createPlan method, because both methods are handling the same URL.

The soultion is to make two different @Path for these methods.