2

I am using jaxb and Date adapter. But the response doesn't contain the date.

@XmlRootElement(name = "project")
public class Project implements Serializable {

    private static final long serialVersionUID = -6462790408417409266L;
    private long projectId;
    private long userId;
    private String projectName;
    private Date createdDate;
    private Date lastUpdatedDate;

    public final long getProjectId() {
        return projectId;
    }
    public final void setProjectId(final long projectId) {
        this.projectId = projectId;
    }
    public final long getUserId() {
        return userId;
    }
    public final void setUserId(long userId) {
        this.userId = userId;
    }
    public final String getProjectName() {
        return projectName;
    }
    public final void setProjectName(final String projectName) {
        this.projectName = projectName;
    }

    /**
     * @return the createdDate
     */
    @XmlElement(name = "createdDate")
    @XmlJavaTypeAdapter(value = DateAdapter.class, type = Date.class)
    public final Date getCreatedDate() {
        return createdDate;
    }

    /**
     * @param createdDate
     *            the createdDate to set
     */
    public final void setCreatedDate(final Date createdDate) {
        this.createdDate = createdDate;
    }

    /**
     * @return the lastUpdatedDate
     */
    @XmlElement(name = "lastUpdatedDate")
    @XmlJavaTypeAdapter(value = DateAdapter.class, type = Date.class)
    public final Date getLastUpdatedDate() {
        return lastUpdatedDate;
    }

    /**
     * @param lastUpdatedDate
     *            the lastUpdatedDate to set
     */
    public final void setLastUpdatedDate(final Date lastUpdatedDate) {
        this.lastUpdatedDate = lastUpdatedDate;
    }

}

And following is my Adapter class,

public class DateAdapter extends XmlAdapter<String, Date> {

    private final SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss");

    @Override
    public String marshal(Date v) throws Exception {
        return dateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws Exception {
        return dateFormat.parse(v);
    }

}

When I call the resful service through a rest client what I get is the following,

{"project":{"projectId":1,"projectName":"testProject","userId":1}}

It doesn't contain the date. What am I missing here?

UPDATE:

I have deployed this in WSO2 application server in CXF environment. And following is the service exposed method definition,

@GET
@Path("{userId}/{projectName}")
@Produces("application/json")
public Project getProjectByUser(@PathParam("userId") long userId,
        @PathParam("projectName") String projectName) throws IOException;
Community
  • 1
  • 1
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
  • Can you provide more information like your dependencies, what sever you're using, and your resource class to test? Looks like a possible jettison problem or overriding the default ObjectMapper. With Jacskon (and JAXB annotation support enabled), this works fine for me. – Paul Samsotha Apr 08 '15 at 04:56
  • 1
    I don't use WSO2 or CXF, but look into how to configure JSON support using your own provider (and possibly disabling the current/default JSON provider), and see if you can configure [this one](https://github.com/FasterXML/jackson-jaxrs-providers) (JacksonJaxbJsonProvider in particular). This is what I used to test with Jersey and it works fine. – Paul Samsotha Apr 08 '15 at 05:15

1 Answers1

1

Have you tried annotating the attribute instead of the method:

@XmlElement(name = "createdDate")
@XmlJavaTypeAdapter(value = DateAdapter.class, type = Date.class)
private Date createdDate;

I've tried in Wildfly 8 and it worked fine.

marciowerner
  • 508
  • 2
  • 7
  • 7