0

I am using Jersey + Jackson + Guice for my webapp. Now I wanted to implemented a simple REST call for my client where i receive arbitrary JSON data on the server, but every time i get the following exception:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "validTo" (Class org.codehaus.jettison.json.JSONObject), not marked as ignorable| at [Source: org.eclipse.jetty.server.HttpConnection$Input@1cafa346; line: 1, column: 25] (through reference chain: org.codehaus.jettison.json.JSONObject["validTo"])

My method signature looks like the following:

@Override
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void post(JSONObject json) throws JSONException {

}

My Guice config:

return Guice.createInjector(new TTShiroModule(this.servletContext),  ShiroWebModule.guiceFilterModule(),
        new ServiceModule(), new JerseyServletModule() {

          @Override
          protected void configureServlets() {
            bind(GuiceContainer.class);

            bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
            bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);

            serve("/rest/*").with(GuiceContainer.class, params);
          }

          @Provides
          @Singleton
          ObjectMapper objectMapper() {
            final ObjectMapper mapper = new ObjectMapper();
            return mapper;
          }

          @Provides
          @Singleton
          JacksonJsonProvider jacksonJsonProvider(ObjectMapper mapper) {
            return new JacksonJsonProvider(mapper);
          }
        });

I searched for this exception a long time but couldnt find any help. I also tried different approaches but wasnt able to resolve this issue.

Anyone can help me?

If you need more information, then please let me know!

best regards.

maxseam
  • 1
  • 1
  • have you seen this http://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable? – Algorithmist Jul 13 '13 at 14:46
  • Thank you for your reply. Ive already seen this question, but it doesnt solve my problem. I dont like to write a "wrapper class" for the JSON i receive on the server, but instead have some generic datatype like JSONObject to receive arbitrary JSON objects which dont rely on any mapping. – maxseam Jul 15 '13 at 12:26

1 Answers1

0

Jersey won't automatically unwrap the json string to JSONObject on its own, but you could easily do as follows:

@Override
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void post(String json) throws JSONException {
   JSONObject object = new JSONObject(json);

   // do things with object
}
cyberz
  • 884
  • 1
  • 8
  • 16
  • ok, thank you i will try that! I thought Jersey maybe can do that automatically. What do you think about that? Is it better to use String in conjunction with JSONObject to transfer arbitrary JSON data to the server or are DTOs the way to go in this case? – maxseam Aug 19 '13 at 08:22
  • Use DTOs if you have them, when dealing with structures defined at runtime you may not have them so this approach can be better than encapsulating everything in a generic carry-everything DTO – cyberz Aug 19 '13 at 09:16