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

0

I need to consume a web service (WS) REST in my android application. The web service was made with glassfish, jersey and ORM hibernate to communicate with the database. Here 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;
    }
}

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

  

Could not read [class pojos.Vegetarian]

And the following cause:

  

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

Follow the Android code below:

Vegetarian consumer:     public class VegetarianConsumer {

    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 <dataNascimento> is being filled. Does anyone know anything that might help me?

    
asked by anonymous 09.11.2016 / 00:54

1 answer

0

I solved my problem by using annotation @XmlJavaTypeAdapter and creating a DateFormatterAdapter to return the date in a valid format to consume in my android yyyy-mm-dd application.

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);
        }
    }
}
    
12.11.2016 / 14:36