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 5984069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:17+00:00 2026-05-22T22:19:17+00:00

I attempting to take a response from a REST service that queries an instance

  • 0

I attempting to take a response from a REST service that queries an instance of MongoDB and parse the response into a Java object. The web service returns the response with a MIME type of html with a newline character separating each record that is returned (although I have the ability to adjust what the service returns). What is the easiest/most efficient way for converting the BSON response into a Java object? I have already created a template class in Java to store the data.

Thanks in advance!

edit: A colleague suggested to me using the MongoDB Java driver’s BSON parsing utilities in the webservice itself and then returning a nicely formatted HTML string. This still leaves me with parsing to do in my application, but will function as a workaround for the time being. Still looking for a way to easily deserialize the BSON response to a Java object.

  • 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-05-22T22:19:18+00:00Added an answer on May 22, 2026 at 10:19 pm

    For those interested, I found the solution to my problem. It turns out that the BSON format can be parsed just like JSON using Google’s GSON driver. The one tricky part that I had to deal with was figuring a way to store nested fields in my template class. The way to allow GSON to parse nested documents is to declare static inner classes in your template class. Here is an example:

    public BSONObject {
       // Private fields
       private int foo;
       private String bar;
    
      // Constructors
      public BSONObject() {}
    
      // Static inner subclasses
      private Widget widget;
      private Duck quack;
    
      // Getters & Setters for outer class
      public int getFoo() {...}
      public String getBar() {...}
      public Widget getWidget() {...}
      public Duck getDuck() {...}
    
      // Static inner class declarations
      public static Widget {
         // include vars & getters/setters
      }
    

    etc.

    Declaring the template class following the above framework allowed me to easily parse MongoDB’s formatting using a few lines of code from the GSON library. Please note that I concatenated a “\n” to each entry when returning data from my webservice so as to separate each document in Mongo’s BSON response:

    public String getNestedField() {
       Gson gson = new Gson();
       String [] split = response.split("\n");
       JsonParser p = new JsonParser();
       String json = split[0];
       BSONObject b = gson.fromJson(p.parse(json), BSONObject.class);
       return b.getWidget().getField();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to take a list of objects, and turn that list into
I am attempting to take the Name and ID fields from each object, but
I'm attempting to take values from a XML file and put them into a
I'm attempting to put together a makefile that will take source files from a
Basically Im attempting to take a php / jquery / css / html web
Attempting to print out a list of values from 2 different variables that are
TiSDK 1.7.1 iOS 5.0 I'm attempting to take the URL of an image that
Im attempting to take a string that is in a partial comma separated setup.
I'm attempting to take 4-5 fields from a large django form and display them
I'm attempting to take large (huge) images (from a digital camera), and convert them

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.