Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:31:20+00:00 2026-06-15T07:31:20+00:00

I’m trying to run batch requests against Facebook with Jersey. The problem is Facebook

  • 0

I’m trying to run batch requests against Facebook with Jersey. The problem is Facebook returns the strangest of structures – a mix of JSONObject and JSONString:

[
   {
      "code": 200,
      "headers": [
         {
            "name": "Access-Control-Allow-Origin",
            "value": "*"
         },
         <!-- some more headers... -->
      ],
      "body": "{\n   \"message\": \"Hello World!\",\n   \"id\": \"...\",\n   \"created_time\": \"2012-10-17T07:18:02+0000\"\n 
                   <!-- ... -->
               }"
   }
]

Now when I try to use the Jackson ObjectMapper to deserialize this mess, I get a

JsonMappingException: Can not instantiate value of type [simple type, class package.to.Post] from JSON String; no single-String constructor/factory method (through reference chain: package.to.BatchResult["body"])

This is the POJO structure I’m using:

public class BatchResult<T> {

    private T body;
    private int code;
    private List<BatchResultHeader> headers;
    // ...
}

public class BatchResultHeader {

    private String name;
    private String value;
    // ...
}

public class Post {

    private String message;
    // ...
}

I send the batch request like this. Params contains the batch parameter and the batch request as defined in the documentation. Also required is the POST call for batch requests. So like I said the call should be fine, as the resulting JSON is as expected (see above):

Client client = Client.create();
WebResource webResource = client.resource("https://graph.facebook.com");
ClientResponse response = webResource.type("application/x-www-form-urlencoded")
            .post(ClientResponse.class, params);
String json = response.getEntity(String.class);

Now I just use the ObjectMapper to deserialize:

TypeReference<List<BatchResult<Post>>> ref = new TypeReference<List<BatchResult<Post>>>(){});
ObjectMapper mapper = new ObjectMapper();
List<BatchResult<Post>> batchResults = mapper.readValue(json, ref);

Using @JsonCreator?

So when search for this exception I found the suggestion to use the @JsonCreator annotation with my Post constructor. That however leads to a

java.lang.IllegalArgumentException: Argument #0 of constructor [constructor for package.to.Post, annotations: {interface org.codehaus.jackson.annotate.JsonCreator=@org.codehaus.jackson.annotate.JsonCreator()}] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator

The solution to this seems to be to annotate each of the POJO’s properties individually and that’s the point where I said: No thank you, certainly not!

So the question remains: Is there any way to deserialize this mix maybe with an ObjectMapper setting?

Or maybe I can tell Jersey to filter the incoming formatted String and cut all the whitespaces before sending it to Jackson for deserialization?


My unsatisfying Workaround:

When I tell my BatchResult class that body is a String, it works and I get a BatchResult with a String body. Now using the ObjectMapper again on that String body with the Post.class type it deserializes correctly. This is my current solution but it looks messy and it should not be my job to iterate over the deserialization results to deserialize its elements… Why can’t Jackson figure this out on its own?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:31:22+00:00Added an answer on June 15, 2026 at 7:31 am

    Just Getting the Body as a Post

    If you are not looking to deserialize the body, then you just need to give the Post object a constructor that takes type string:

    public class Post {
        public Post( String message ) {
          this.message = message;
        }
        private String message;
        // ...
    }
    

    Unmarshalling the Body into Post

    If you would instead like to populate the Post object with the JSON contained in the string, you can use a JsonDeserializer. First, you will need to define a deserializer that reads in the string value and then unmarshalls it. Here is an example implementation:

    import java.io.IOException;
    
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.JsonToken;
    import org.codehaus.jackson.ObjectCodec;
    import org.codehaus.jackson.map.BeanProperty;
    import org.codehaus.jackson.map.ContextualDeserializer;
    import org.codehaus.jackson.map.DeserializationConfig;
    import org.codehaus.jackson.map.DeserializationContext;
    import org.codehaus.jackson.map.JsonDeserializer;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.ObjectReader;
    
    public class EmbeddedJsonDeserializer
      extends JsonDeserializer<Object>
      implements ContextualDeserializer<Object>
    {
      Class<?> type = null;
      public EmbeddedJsonDeserializer() {
      }
    
      public EmbeddedJsonDeserializer( Class<?> type ) {
        this.type = type;
      }
    
      @Override
      public Object deserialize(JsonParser parser, DeserializationContext context)
          throws IOException, JsonProcessingException {
        JsonToken curr = parser.getCurrentToken();
        if (curr == JsonToken.VALUE_STRING) {
          if( type == null ) return parser.getText();
          ObjectCodec codec = parser.getCodec();
          if( codec == null ) {
            return new ObjectMapper().readValue(parser.getText(), type);
          }
          else if( codec instanceof ObjectMapper ) {
            return ((ObjectMapper)codec).readValue(parser.getText(), type);
          }
          else if( codec instanceof ObjectReader ) {
            return ((ObjectReader)codec).withType(type).readValue(parser.getText());
          }
          else {
            return new ObjectMapper().readValue(parser.getText(), type);
          }
        }
        throw context.mappingException(type);
      }
    
      public JsonDeserializer<Object> createContextual(DeserializationConfig config, BeanProperty property) throws JsonMappingException {
        return new EmbeddedJsonDeserializer(property.getType().getRawClass());
      }
    }
    

    Then you need to add a @JsonDeserialize annotation to the body field, specifying the deserializer to use:

    public class BatchResult<T> {
      @JsonDeserialize(using=EmbeddedJsonDeserializer.class)
      private T body;
      ...
    

    You should now be able to get embedded JSON into your Post objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.