I need to generate a generic list
List<?> listaGenerica;
having as a parameter a string with the class path, as shown below.
Class class = Class.forName("com.teste.Endereco");
Is this possible?
Sorry for the confusion in the description of the question because I'm confused really..kkk
I have the following hibernate entity class.
@TypeDefs({
@TypeDef(name = "jsonbEndereco", typeClass = JsonBinaryType.class})
})
@Entity
@Table(name = "teste")
@SequenceGenerator(name = "teste_id_seq", sequenceName = "teste_id_seq", allocationSize = 1)
public class Teste implements Serializable {
private static final long serialVersionUID = 6800342991944554204L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "teste_id_seq")
private Integer id;
private String nome;
private String cpf;
@Type(type = "jsonbEndereco")
@Column(columnDefinition = "jsonbEndereco")
private List<Endereco> enderecos;
When converting to address list, it converts all fields to string, not allowing the typecast to address.
OBJECT_MAPPER.readValue(string, clazz);
That's because clazz comes as java.util.List
I've solved this problem by using the list where I know it's a List with the following code.
Teste teste = testeCTR.carregar(1);
Endereco endereco = null;
ObjectMapper mapper = new ObjectMapper();
List<Endereco> enderecos = mapper.convertValue(teste.getEnderecos(), new TypeReference<List<Endereco>>() {});
My intention was to pass as parameter br.com.jenkins.Endereco
@TypeDefs({
@TypeDef(name = "jsonbEndereco", typeClass = JsonBinaryType.class, parameters = {
@Parameter(name = JsonBinaryType.CLASS, value = "br.com.jenkins.Endereco")})
})
and when converting json wanted to convert and map correctly.
OBJECT_MAPPER.readValue(string, new TypeReference<List<com.teste.Endereco>>() {});
I'm sorry if you're still confused ...
[Resolved in parts]
Create a list of objects from a Type of a class I could not get ... But I bypassed the error Creating an E-List class that contains an address list, and that's how it worked.
public class ListaEndereco {
private List<Endereco> enderecos;
public List<Endereco> getEnderecos() {
return enderecos;
}
public void setEnderecos(List<Endereco> enderecos) {
this.enderecos = enderecos;
}
}
public class Endereco {
private Integer enderecoid;
private String endereco;
public Integer getEnderecoid() {
return enderecoid;
}
public void setEnderecoid(Integer enderecoid) {
this.enderecoid = enderecoid;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
}
Type type = ListaEndereco.class;
String json = "{\"enderecos\": [{\"endereco\": \"Teste\", \"enderecoid\": 1}, {\"endereco\": \"Teste2\", \"enderecoid\": 2}, {\"endereco\": \"Teste3\", \"enderecoid\": 3}]}";
ListaEndereco lista = (ListaEndereco) OBJECT_MAPPER.readValue(json, type.getClass());
Thank you very much to everyone. For help