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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:22:20+00:00 2026-06-08T21:22:20+00:00

I have a Java class annotated with @XmlRootElement . This Java class has a

  • 0

I have a Java class annotated with @XmlRootElement. This Java class has a long property (private long id) that I want to return to a JavaScript-client.

I create the JSON as follows:

MyEntity myInstance = new MyEntity("Benny Neugebauer", 2517564202727464120);
StringWriter writer = new StringWriter();
JSONConfiguration config = JSONConfiguration.natural().build();
Class[] types = {MyEntity.class};
JSONJAXBContext context = new JSONJAXBContext(config, types);
JSONMarshaller marshaller = context.createJSONMarshaller();
marshaller.marshallToJSON(myInstance, writer);
json = writer.toString();
System.out.println(writer.toString());

This will be generated:

{"name":"Benny Neugebauer","id":2517564202727464120}

But the problem is that the long value is too large for the JavaScript client. Therefore, I would like to return this value as a string (without making the long a string in Java).

Is there an annotation (or something similar) that can generate the following?

{"name":"Benny Neugebauer","id":"2517564202727464120"}
  • 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-08T21:22:21+00:00Added an answer on June 8, 2026 at 9:22 pm

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

    Below is how you could accomplish this use case with MOXy as your JSON provider.

    MyEntity

    You would annotate your long property with @XmlSchemaType(name="string") to indicate that it should be marshalled as a String.

    package forum11737597;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class MyEntity {
    
        private String name;
        private long id;
    
        public MyEntity() {
        }
    
        public MyEntity(String name, long id) {
            setName(name);
            setId(id);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @XmlSchemaType(name="string")
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
    }
    

    jaxb.properties

    To configure MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    I have modified your sample code to show what it would look like if you used MOXy.

    package forum11737597;
    
    import java.io.StringWriter;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            MyEntity myInstance = new MyEntity("Benny Neugebauer", 2517564202727464120L);
            StringWriter writer = new StringWriter();
            Map<String, Object> config = new HashMap<String, Object>(2);
            config.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            config.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            Class[] types = {MyEntity.class};
            JAXBContext context = JAXBContext.newInstance(types, config);
            Marshaller marshaller = context.createMarshaller();
            marshaller.marshal(myInstance, writer);
            System.out.println(writer.toString());
        }
    
    }
    

    Output

    Below is the output from running the demo code:

    {"id":"2517564202727464120","name":"Benny Neugebauer"}
    

    For More Information

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

Sidebar

Related Questions

I have a Java class which has some field annotated with @SearchCriteria(criteria = class1.class2.field)
I have a Person class (annotated with @XmlRootElement ) in Java with two properties
I'm working with java. I have an interface that has an annotated method in
I have a Java class MyPojo that I am interested in deserializing from JSON.
If I have a java class which is package-private (declared with class, not public
I have a java project that runs on a webserver. I always hit this
I have a service class implemented in Java 6 / Spring 3 that needs
I have a single java bean, which is already annotated for JPA, that I
I have written a java annotation that looks like this: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) // can
I have a MyBean annotated @XmlRootElement public class MyBean ... Marshalling/Unmarshalling MyBean w/o problems,

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.