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

  • Home
  • SEARCH
  • 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 7743433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:31:59+00:00 2026-06-01T09:31:59+00:00

This is quite straight forward. What is the minimum required structure for a Java

  • 0

This is quite straight forward. What is the minimum required structure for a Java POJO to be marshalled as a JSON?

Can you marshall an object as a JSON if it has only getters/setters or are the field declarations mandatory?

Setter/Getter example:

class Circle{
 private float radius;
 private float pi;

 // setter and getters for those aboce;

 public float getArea(){
 // returns the computed area;
 }
}

So can such an object be marshalled as a JSON if the “area” field is not defined in the Foo class as a field? Or is it mandatory to explicitly declare all fields in your POJO?

  • 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-01T09:32:00+00:00Added an answer on June 1, 2026 at 9:32 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

    Circle

    For MOXy the only annotation required would be @XmlElement on the area property as there is no setter corresponding to the getter. The @XmlElement is included in Java SE 6 and above:

    package forum10028037;
    
    import javax.xml.bind.annotation.XmlElement;
    
    class Circle{
    
        private float radius;
        private float pi;
    
        public float getRadius() {
            return radius;
        }
    
        public void setRadius(float radius) {
            this.radius = radius;
        }
    
        public float getPi() {
            return pi;
        }
    
        public void setPi(float pi) {
            this.pi = pi;
        }
    
        @XmlElement
        public float getArea(){
            return pi * radius * radius;
        }
    
    }
    

    Demo

    package forum10028037;
    
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Circle.class);
    
            Circle circle = new Circle();
            circle.setPi(3.14f);
            circle.setRadius(10.1f);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty("eclipselink.media-type", "application/json");
            marshaller.setProperty("eclipselink.json.include-root", false);
            marshaller.marshal(circle, System.out);
        }
    
    }
    

    Output

    {
       "area" : 320.31143,
       "pi" : 3.14,
       "radius" : 10.1
    }
    

    For More Information

    • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
    • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is quite a straight forward question that I can't seem to find a
This is probably quite straight-forward but at the moment I can't think of a
This is quite straight forward. I'm calling a web-service (.asmx, with session enabled) from
This isn't quite as straight forward as one may think. I'm using a plugin
The case is quite straight forward (in my mind). The client has a native
I'm sure that this is very straight-forward, however, after scouring the net I can't
i'm quite new with php. i think this is quite straight forward but i'm
This is quite weird but my search function can search for any word except
This is a fairly straight forward decorator pattern scenario, with the complication that the
I'm a beginner jquery user so I'm really hoping this will be quite straight

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.