2

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?

Community
  • 1
  • 1
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

0

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 remove that annotation from first parameter, and add second parameter annotated with @BeanParam:

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

and then implement your ExtraTest class with additional fields mappings:

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

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

@BeanParam is the feature that allows you to easily access additional request information (header fields, query params etc.). It couldn't be mixed with your json data object mapping.

More information:

https://abhirockzz.wordpress.com/2014/07/21/new-in-jax-rs-2-0-beanparam-annotation/

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

Maciej Marczuk
  • 3,593
  • 29
  • 28
  • When should I accept a Json object without the BeanParam annotation and when should I use it? – CodeMonkey Aug 21 '16 at 10:53
  • Check my edited answer. `@BeanParam` is a mechanism that allows you to easy access additional request information (headers, query params etc.). – Maciej Marczuk Aug 21 '16 at 10:56