Error while consuming Glassfish + Jersey REST web service with date that returns an xml

Asked

Viewed 95 times

0

I need to consume a web service (WS) REST on my android app. The web service was done with glassfish, jersey and the Hibernate ORM to communicate with the database. Below is the WS code:

Vegetarian Service:

@Path("/vegetariano")
public class VegetarianoService {

    @GET
    @Path("buscar/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Vegetariano consultar(@PathParam("id") int id) {
        return new Vegetariano("Lucas Kauer", new Date(), Genero.MASCULINO, "1234", "123", TipoDeVegetariano.LACTOVEGETARIANO,
                new ArrayList<Alergia>(), "[email protected]", "aa123");

    }
}

Vegetarian

@XmlRootElement

@Entity
@Table(name = "Vegetariano")
public class Vegetariano{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Basic(optional = false)
    @Column(length = 100)
    private String nomeCompleto;
    @Basic(optional = false)
    @Temporal(TemporalType.DATE)
    private Date dataNascimento;
    @Basic(optional = false)
    @Enumerated(EnumType.STRING)
    private Genero genero;
    @Basic(optional = false)
    @Column(length = 9)
    private String cep;
    @Basic(optional = true)
    @Column(length = 50)
    private String complemento;
    @Enumerated(EnumType.STRING)
    @Basic(optional = false)
    private TipoDeVegetariano tipoVegetariano;
    @Basic(optional = false)
    private String email;
    @Basic(optional = false)
    @Column(length = 10)
    private String senha;
    @Basic(optional = true)
    @OneToMany(mappedBy = "usuarioVegetariano", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Alergia.class)
    private List<Alergia> alergias = new ArrayList<Alergia>();
    @Basic(optional = true)
    @OneToMany(fetch = FetchType.EAGER, targetEntity = Diario.class)
    private List<Diario> diarios = new ArrayList<Diario>();

    public Vegetariano() { }

    public Vegetariano(String nome, Date dataNascimento, Genero genero, String cep, String complemento, 
                       TipoDeVegetariano tipoVegetariano, List<Alergia> alergias, String email, String senha){
        this.nomeCompleto = nome;
        this.dataNascimento = dataNascimento;
        this.genero = genero;
        this.cep = cep;
        this.complemento = complemento;
        this.tipoVegetariano = tipoVegetariano;
        this.alergias = alergias;
        this.email = email;
        this.senha = senha;
    }
}

While trying to consume in my android application occurs the following error:

Could not read [class pojos. Vegetarian]

And the following cause:

Unparseable date: "2016-11-08T21:43:00.116-02:00" (at offset 10)

Below is the code of Android:

Consumer of Vegetarian: public class Vegetarianoconsumer {

    private RestTemplate restTemplate;
    private static final String URL_BASE = "http://10.0.2.2:8080/Rest2016/vegeryday/vegetariano";

    public VegetarianoConsumer(){
        this.restTemplate = new RestTemplate();
        this.restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
    }

    public Vegetariano chamaMetodoConsultar(int id){
        String URL = URL_BASE + "/buscar/{id}";
        Map<String, Integer> meuMap = new HashMap<>();
        meuMap.put("id", id);

        Vegetariano vegetariano = restTemplate.getForObject(URL, Vegetariano.class, meuMap);
        return vegetariano;
    }
}

Vegetarian:

public class Vegetariano{

    private int id;
    private String nomeCompleto;
    private Date dataNascimento;
    private Genero genero;
    private String cep;
    private String complemento;
    private TipoDeVegetariano tipoVegetariano;
    private String email;
    private String senha;
    private List<Alergia> alergias = new ArrayList<Alergia>();
    private List<Diario> diarios = new ArrayList<Diario>();
    private Vegetariano() {}

    public Vegetariano(String nome, Date dataNascimento, Genero genero, String cep, String complemento,
                       TipoDeVegetariano tipoVegetariano, List<Alergia> alergias, String email, String senha){
        this.nomeCompleto = nome;
        this.dataNascimento = dataNascimento;
        this.genero = genero;
        this.cep = cep;
        this.complemento = complemento;
        this.tipoVegetariano = tipoVegetariano;
        this.alergias = alergias;
        this.email = email;
        this.senha = senha;
    }
}

The web service is returning the following XML:

<vegetariano>
  <cep>1234</cep>
  <complemento>123</complemento>
  <dataNascimento>2016-11-08T21:44:08.398-02:00</dataNascimento>
  <email>[email protected]</email>
  <genero>MASCULINO</genero>
  <id>0</id>
  <nomeCompleto>Lucas Kauer</nomeCompleto>
  <senha>aa123</senha>
  <tipoVegetariano>LACTOVEGETARIANO</tipoVegetariano>
</vegetariano>

I believe the problem is the format in which the <dataNascimento> is being filled in. Does anyone know anything that can help me?

1 answer

0


I solved my problem by making use of Annotation @XmlJavaTypeAdapter and creating a DateFormatterAdapter to return the date in a valid format to consume on my android app yyyy-mm-dd.

public class Vegetariano {

    ...

    @XmlJavaTypeAdapter(DateFormatterAdapter.class)
    public Date getDataNascimento() {
        return dataNascimento;
    }

    ... 

    private static class DateFormatterAdapter extends XmlAdapter<String, Date> {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.