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?