Yonatan Nir Yonatan Nir - 3 months ago 43
Java Question

Json object sent by postman received with null members

This is a follow up question to this question:

sending nested json object using postman

I am trying to send a Json object to a Jersey web service using Postman.

I am doing it like this:

On the Jersey side:

@Path("/testMethod")
@POST
@UnitOfWork
public short testMethod(@NotNull @BeanParam Test test)
{ ... }


Test class is a simple class:

public class Test
{
public String field;

public Test()
{

}
}


On the postman side I'm sending a POST message with Body set as raw and the content type is set as Json(application/json). The body itself is:

{
"field" : "12"
}


When sending this request, the
field
in the received parameter is null... why is that?

Answer

If you just want to map your json data to object, then just remove @BeanParam and it will work properly.

However, if you want to use @BeanParam feature, you should implement something like this:

@Path("/testMethod")
@POST
@UnitOfWork
public short testMethod(@NotNull Test test, @BeanParam ExtraTest extraTest)
{ ... }

and your ExtraTest class with additional fields mappings:

public class ExtraTest
{
    @HeaderParam("Accept")
    public String acceptHeader;

    /* 
    your other header params, path params and so on...
    ...
    */
} 

More information: https://jersey.java.net/apidocs/2.22/jersey/javax/ws/rs/BeanParam.html