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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:34:45+00:00 2026-05-31T00:34:45+00:00

I am currently working on a RESTful API. I have an Employee class and

  • 0

I am currently working on a RESTful API. I have an Employee class and an EmployeeResource class. I also have a custom DateAdapter, which changes my Date properties to Long timestamps. However, my JSON responses are showing the timestamps as strings (wrapped in double quotes) rather than numbers (without double quotes). Here is an abbreviated version of my code and captured JSON response…

Custom DateAdapter

public class DateAdapter extends XmlAdapter<Long, Date> {
    @Override
    public Date unmarshal(Long v) throws Exception {
        return new Date(Long.valueOf(v));  
    }
    @Override
    public Long marshal(Date v) throws Exception {
        return v.getTime();  
    }
}

Entity Class

@Entity
@javax.xml.bind.annotation.XmlRootElement
@XmlType(propOrder={"createdOn","empId"})
public class Employee implements Serializable {
    private Date createdOn;
    private Integer empId;

    @Column(nullable=false)
    @Temporal(TemporalType.TIMESTAMP)
    @XmlJavaTypeAdapter(DateAdapter.class)
    public Date getCreatedOn() {
        return createdOn;
    }
    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    @Id
    @XmlID
    public Integer getEmpId() {
        return empId;
    }
    public void setEmpId(Integer empId) {
        this.empId = empId;
    }
}

EmployeeResource

@Path("/Employees")
@javax.xml.bind.annotation.XmlRootElement 
@XmlType(propOrder={"hateoas","employees"})
public class EmployeeResource {
    List<Employee> employees;

    public List<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Response getEmployee(@Context UriInfo ui, @PathParam("id") Integer id) {
        Session session = HibernateUtil.getSession();
        session.beginTransaction();
        Criteria criteria=session.createCriteria(Employee.class);
        criteria.add(Restrictions.eq("empId", new Integer(10150)));
        this.employees = criteria.list();
        return Response.ok(this).build();
    }
}

Current JSON response

{
  "employees":{
    "createdOn":"1330915130163",
    "empId":"10150"
  }
}

Expected JSON response

{
  "employees":{
    "createdOn":1330915130163,
    "empId":10150
  }
}

I’m assuming that there’s some way to prevent JAXB or JAX-RS from wrapping all numbers in quotes. Could someone guide me to where I could configure this?

Thanks in advance!

EDIT #1 2012.03.07
Ok, so after some more researching, I think my problem is with the default JSONConfiguration.Notation being used, MAPPED . It looks like the NATURAL JSONConfiguration.Notation would get me what I want. However, I haven’t found a clear example of how to apply that application wide. I’m assuming I would specify this in my ApplicationConfig class that extends javax.ws.rs.core.Application .

EDIT #2 2012.03.10
Ok, so after some more researching I decided to use the JSON parser library, Jackson. It seems to be the most complete JSON solution out there using just the default configuration. By default Dates are translated to their corresponding timestamps and serialized as numbers (w/o quotes). The only drawback I have come across is that Jackson currently does not support the JAXB annotations, “@XmlID” and “@XmlIDREF”. Since I have direct self-references in my data model (not shown above), I have created another question to discuss. If anyone is interested click here to follow that thread…

  • 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-31T00:34:46+00:00Added an answer on May 31, 2026 at 12:34 am

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

    The JAX-RS implementation you are using may be using a JAXB (JSR-222) implementation with something like Jettison to produce JSON. Jettison provides a StAX API to interact with JSON, since that StAX APIs don’t have any sort of typing WRT text, all the simple values get treated as strings:

    • http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html

    To get the behaviour you are looking for you can use a different binding solution. We are adding this support into the MOXy component for EclipseLink 2.4:

    • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

    To configure MOXy as your JSON-binding provider in a JAX-RS environment, you could create a MessageBodyReader/MessageBodyWriter that looks like:

    import java.io.*;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;
    import java.util.Collection;
    import javax.xml.transform.stream.StreamSource;
    
    import javax.ws.rs.*;
    import javax.ws.rs.core.*;
    import javax.ws.rs.ext.*;
    import javax.xml.bind.*;
    
    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public class MOXyJSONProvider implements 
        MessageBodyReader<Object>, MessageBodyWriter<Object>{
    
        @Context
        protected Providers providers;
    
        public boolean isReadable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
            return true;
        }
    
        public Object readFrom(Class<Object> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
            throws IOException, WebApplicationException {
            try {
                Unmarshaller u = getJAXBContext(type, mediaType).createUnmarshaller();
                u.setProperty("eclipselink.media-type", mediaType.toString());
                u.setProperty("eclipselink.json.include-root", false);//tiny fix
                return u.unmarshal(new StreamSource(entityStream), (Class) genericType);
            } catch(JAXBException jaxbException) {
                throw new WebApplicationException(jaxbException);
            }
        }
    
        public boolean isWriteable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
            return true;
        }
    
        public void writeTo(Object object, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException,
            WebApplicationException {
            try {
                Marshaller m = getJAXBContext(Customer.class, mediaType).createMarshaller();
                m.setProperty("eclipselink.media-type", mediaType.toString());
                m.setProperty("eclipselink.json.include-root", false);
                m.marshal(object, entityStream);
            } catch(JAXBException jaxbException) {
                throw new WebApplicationException(jaxbException);
            }
        }
    
        public long getSize(Object t, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
            return -1;
        }
    
        private JAXBContext getJAXBContext(Class<?> type, MediaType mediaType) 
            throws JAXBException {
            ContextResolver<JAXBContext> resolver 
                = providers.getContextResolver(JAXBContext.class, mediaType);
            JAXBContext jaxbContext;
            if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
                return JAXBContext.newInstance(type);
            } else {
                return jaxbContext;
            }
        }
    
    }
    

    For More Information

    • https://stackoverflow.com/a/8989659/383861
    • http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-15.html
    • http://blog.bdoughan.com/2011/04/moxys-xml-metadata-in-jax-rs-service.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently working on the design of a RESTful application. We have decided
Im currently working with an API which requires we send our collection details in
Currently working on a snippet to input variables in which a user changes a
I'm currently working to specify my company's new partner/public API, which will be a
I have some POJOs which are the basis for this RESTful API I am
I`m currently working on a script, and I have the following situation. function somnicefunction()
Im currently working on a site which will contain a products catalog. I am
I'm currently working on benchmarking a RESTful service I've made, and part of that
I am working on an API of sorts. Its kinda like a RESTful API
Currently working on something which uses ajax for some pagination. What I'm looking to

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.