You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by "Alinezhad.a" <Al...@outlook.com> on 2017/11/02 14:15:02 UTC
XmlJavaTypeAdapter not working
Hi
i'm get confused why this simple code not working, i have this simple class
@Entity
@Table(name = "users")
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
protected Long id;
@NotNull
@Size(min = 3, max = 100, message = "{invalid.fullName}")
@Column(name = "full_name", nullable = false, length = 100)
@XmlElement(name = "fullName")
private String fullName;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_login_date")
@XmlElement(name = "lastLoginDate")
@XmlJavaTypeAdapter(DateFormatterAdapter.class)
private Date lastLoginDate;
public Long getId() { return id;}
public void setId(Long id) { this.id = id; }
public String getFullName() { return fullName; }
public void setFullName(String fullName) { this.fullName = fullName; }
public Date getLastLoginDate() { return lastLoginDate; }
public void setLastLoginDate(Date lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
public User() { }
}
and this simple XmlAdapter as follow
public class DateFormatterAdapter extends XmlAdapter<String, Date> {
public DateFormatterAdapter() { }
@Override
public Date unmarshal(String v) throws Exception {
return new Date(Long.parseLong(v));
}
@Override
public String marshal(Date v) throws Exception {
return ((Long) v.getTime()).toString();
}
}
and a jersey restful service that return list of users as APPLICATION_JSON,
in this scenario every things is ok, but DateFormatterAdapter class not been
called and lastLoginDate not formatted as desired in result.
but when i am doing this as follow manually,every things is Ok
JAXBContext jc = JAXBContext.newInstance(User.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("e:/input.xml");
User foo = (User) unmarshaller.unmarshal(xml);
foo.setLastLoginDate(new Date());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
i am using tomee 7.0.2 as my application server and eclipselink-2.7.0
--
Sent from: http://tomee-openejb.979440.n4.nabble.com/TomEE-Dev-f982480.html
Re: XmlJavaTypeAdapter not working
Posted by "Alinezhad.a" <Al...@outlook.com>.
when i add org.eclipse.persistence.moxy-2.7.0 to my project and add
openejb-jar.xml file this goes ok
openejb-jar.xml
=======================================
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar>
<pojo-deployment class-name="jaxrs-application">
<properties>
# optional but requires to skip scanned providers if set to true
cxf.jaxrs.skip-provider-scanning = false
# list of providers we want
cxf.jaxrs.providers =
org.eclipse.persistence.jaxb.rs.MOXyJsonProvider
</properties>
</pojo-deployment>
</openejb-jar>
=======================================
but now i have a new problem, tomee use MOXyJsonProvider for create
response, but for request it still us johnzon for parsing json data and
create pojo objects.
--
Sent from: http://tomee-openejb.979440.n4.nabble.com/TomEE-Dev-f982480.html
Re: XmlJavaTypeAdapter not working
Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi
is User part of a webservice signature or referenced through a
@XmlSeeAlso? If not then it is not passed to the JAXBContext creation.
Romain Manni-Bucau
@rmannibucau | Blog | Old Blog | Github | LinkedIn
2017-11-02 15:15 GMT+01:00 Alinezhad.a <Al...@outlook.com>:
> Hi
>
> i'm get confused why this simple code not working, i have this simple class
>
> @Entity
> @Table(name = "users")
> @XmlRootElement(name = "user")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class User implements Serializable {
>
> @Id
> @GeneratedValue(strategy = GenerationType.AUTO)
> @Column(name = "id")
> protected Long id;
>
> @NotNull
> @Size(min = 3, max = 100, message = "{invalid.fullName}")
> @Column(name = "full_name", nullable = false, length = 100)
> @XmlElement(name = "fullName")
> private String fullName;
>
> @Temporal(TemporalType.TIMESTAMP)
> @Column(name = "last_login_date")
> @XmlElement(name = "lastLoginDate")
> @XmlJavaTypeAdapter(DateFormatterAdapter.class)
> private Date lastLoginDate;
>
> public Long getId() { return id;}
>
> public void setId(Long id) { this.id = id; }
>
> public String getFullName() { return fullName; }
>
> public void setFullName(String fullName) { this.fullName = fullName; }
>
> public Date getLastLoginDate() { return lastLoginDate; }
>
> public void setLastLoginDate(Date lastLoginDate) {
> this.lastLoginDate = lastLoginDate;
> }
>
> public User() { }
>
> }
>
> and this simple XmlAdapter as follow
>
> public class DateFormatterAdapter extends XmlAdapter<String, Date> {
>
> public DateFormatterAdapter() { }
>
> @Override
> public Date unmarshal(String v) throws Exception {
> return new Date(Long.parseLong(v));
> }
>
> @Override
> public String marshal(Date v) throws Exception {
> return ((Long) v.getTime()).toString();
> }
> }
> and a jersey restful service that return list of users as APPLICATION_JSON,
> in this scenario every things is ok, but DateFormatterAdapter class not been
> called and lastLoginDate not formatted as desired in result.
>
> but when i am doing this as follow manually,every things is Ok
>
> JAXBContext jc = JAXBContext.newInstance(User.class);
>
> Unmarshaller unmarshaller = jc.createUnmarshaller();
> File xml = new File("e:/input.xml");
> User foo = (User) unmarshaller.unmarshal(xml);
> foo.setLastLoginDate(new Date());
>
> Marshaller marshaller = jc.createMarshaller();
> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
> marshaller.marshal(foo, System.out);
>
> i am using tomee 7.0.2 as my application server and eclipselink-2.7.0
>
>
>
>
>
> --
> Sent from: http://tomee-openejb.979440.n4.nabble.com/TomEE-Dev-f982480.html