1

I have a REST service endpoint that accepts JSON in the following format:

{
  "name": "example",
  "properties": {
    "key1": "val1",
    "key2": "val2"
  }
}

Then I have this class

class Document {
  private String name;

  private Map<String, String> properties;

  ... setters/getters
}

And my REST service method

logger = Logger.getLogger("logger");

@POST
@Consumes(MediaType.APPLICATION_JSON)
Response addDocument(Document document) {
  logger.info(document);
  return Response.ok(200).build();
}

But whenever I post the JSON above it's not unmarshalling my properties. It is always null. The name property is mapped correctly..

I would gladly accept any help/clues.Thanks!

EDIT: I did not mention that I am using glassfish 4.1 and what comes with it. I don't have any lib dependency for marshal/unmarshal.

stefo0O0o
  • 119
  • 1
  • 11

2 Answers2

0

I just added Gson and created custom entity provider for JSON. It works now as expected, but I would've been more glad to not handle it myself but let the container do it with whatever is configured for JAXB.

stefo0O0o
  • 119
  • 1
  • 11
0

Your JSON, the Java class the JSON will be parsed into and your JAX-RS resource method look fine:

{
  "name": "example",
  "properties": {
    "key1": "val1",
    "key2": "val2"
  }
}
public class Document {

    private String name;
    private Map<String, String> properties;

    // Getters and setters omitted
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addDocument(Document document) {
    ...
    return Response.ok(200).build();
}

Instead of using Gson as you mentioned in your answer, you could consider using a JSON provider which integrates with Jersey. Jackson is a good choice and Maps should work fine.

For details on how to use Jackson with Jersey, refer to this answer.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359