harry123
harry123

Reputation: 910

postman unable to parse JSON Object

I am using the dropwizard framework and I am trying to hit the PUT via postman request but I am getting:

{
    "code": 400,
    "message": "Unable to process JSON"
} 

POSTMAN request's body is:

[{"v1":".abc",
"v2": false,
"v3": 2,
"v4": ["Other"]}]

My resource file is as follows:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path(value = "/v1/")
public class SomeClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(SomeClass.class);

    @PUT
    @Path(value = "/path/{value}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response addCandidates(@PathParam(value = "value") String value, OtherClass otherclass) {
        LOGGER.info("Request recieved"); // this line is not getting executed
        return Response.status(Response.Status.OK).entity(null).build();
    }
}

Class definitions:

import lombok.Data;
import java.util.List;

@Data
public class OtherClass {
    private List<Something> something;
}


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class Something extends abc {
}

Table schema:

import java.util.Set;

@Data
public class abc implements Comparable<abc> {
    @PrimaryKey
    @JSONField(name = "v1")
    public String v1;

    @JSONField(name = "v2")
    public boolean v2;

    @JSONField(name = "v3")
    public Integer v3;

    @JSONField(name = "v4")
    public Set<String> v4;

}

When I am going to the debug console for the POSTMAN, it shows me the correct populated values. I am not sure if v4 is causing the issue

Upvotes: 0

Views: 2350

Answers (1)

SergeiTonoian
SergeiTonoian

Reputation: 341

Your json is not correct, you are trying to parse an array of object to the object OtherClass which is not an array. So your json should be:

{
    "something": [
        {
            "v1": ".abc",
            "v2": false,
            "v3": 2,
            "v4": [
                "Other"
            ]
        }
    ]
}

Also you are mixing annotations - @JSONField is from FastJson library, but @JsonIgnoreProperties is from Jackson, so make sure you use the same serialiser.

Upvotes: 1

Related Questions