Good morning, I have a problem with the response of rest services in java, I used a generic object type to standardize the answers but when I add objects to this object and it becomes Json it does not convert well, this is the object:
public class Data implements Serializable {
private Object data;
private Long numero_reg;
/**
* @return the data
*/
public Object getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(Object data) {
this.data = data;
}
/**
* @return the numero_reg
*/
public long getNumero_reg() {
return numero_reg;
}
/**
* @param numero_reg the numero_reg to set
*/
public void setNumero_reg(long numero_reg) {
this.numero_reg = numero_reg;
}
}
This is the rest that you use as an example to see if it works or not:
@Path("/pruebas")
public class Pruebita {
@GET
public SigepData getalgo(){
ObjectoDatos od=new ObjectoDatos();
od.setPrueba("hola");
od.setPrueba2("hola2");
Data sd=new SigepData();
sd.setData(od);
sd.setNumero_reg(1);
return sd;
}
}
but the result shown instead of displaying the Data Object shows its instance name as if instead of displaying the object it shows the toString which is not what you want in the response.
{"data":"bo.ObjectoDatos@6d858b63","numero_reg":1}
Please someone has some idea how to do so that json mapee properly?
The only way to make it work has been:
import java.io.Serializable;
public class Data implements Serializable {
private ObjectoDatos data;
private Long numero_reg;
/**
* @return the data
*/
public ObjectoDatos getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(ObjectoDatos data) {
this.data = data;
}
/**
* @return the numero_reg
*/
public long getNumero_reg() {
return numero_reg;
}
/**
* @param numero_reg the numero_reg to set
*/
public void setNumero_reg(long numero_reg) {
this.numero_reg = numero_reg;
}
}
ie changing Object by Data Object and in that case if the rest already returns well:
{"data": {"test": "hello", "test2": "hello2"}, "number_reg": 1}
but I can not do that in the system that I am developing since Data would have to be many objects and can not be used for others, I have also tried to use generic T, there was the problem that I do not know what kind of pass to try hardcodeando I put the type ObjectoDatos but it is not the same problem at the beginning does not recognize it and calls toString, as you see the tostring is not the problem, but it does not recognize the type to convert to json, and as it can not what it does is call the object toString and show that in the json, but as you can see if I specify the object if it works, so I know that it's not the toString but the json's typed and mapped