GOOGLE ADS

martes, 26 de abril de 2022

¿Producir JSON por el servicio web RESTful en Spring Boot?

Mi problema: no devuelvo Json sino una matriz.

Entonces, desearé que Json regrese:

Mi interfaz de repositorio:

public interface SuiRepository extends JpaRepository<Folder, Integer>{
@Query("...")
public List<Folder> data();
}

Mi método:

@Override
public List<Folder> getFolder(){
List<Folder> s = folderRepository.data();
return s;
}

Servicio Mi Descanso:

@RequestMapping(value="/folders", method=RequestMethod.GET, produces="application/json", consumes="application/json")
@ResponseBody
public List<Folder> getFolders() {
return iUd.getFolders();
}

Mi clase de carpeta

Entity
public class Folder implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idFolder;
private String comments;
@ManyToOne
@JoinColumn(name="IdFile")
private File file;
@ManyToOne
@JoinColumn(name="username")
private User user;
**Getters&Setters...**
}

El retorno actual:

[["Ban","dee","dede@gmail.com",1,"xx","Emb"],["Cin","mis","sse@gmail.com",1,"yy","Ns"]]

¡Gracias!


Solución del problema

Puede usar un constructor anotado con @JsonCreator en su entidad:

Ex

...
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String phone;
String password;
@JsonCreator
public User(@JsonProperty("name") String name,
@JsonProperty("phone") String phone) {
this.name = name;
this.phone = phone;
}
...

No hay comentarios.:

Publicar un comentario

Flutter: error de rango al acceder a la respuesta JSON

Estoy accediendo a una respuesta JSON con la siguiente estructura. { "fullName": "FirstName LastName", "listings...