簡體   English   中英

Spring Boot和Json對象

[英]Spring Boot and Json Object

我正在使用Spring Boot,但我遇到下一個問題:

有一個班級模型:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ModelPessoa extends UtilBaseEntities<long>// . . .

實體:

@Entity
@Table(name = "aluno")
public class EntityAluno extends ModelPessoa / / . . .

以及帶人的類User(pessoa):

@Entity
@Table(name = "usuario")
public class EntityUsuario extends UtilBaseEntities<long> / / . . .
@OneToOne
@JoinColumn(name = "id_pessoa")
private ModelPessoa pessoa;

但是..用我的控制器保存時:

@RestController
public class ControllerAluno {
@Autowired IRepositoryAluno repository;
@RequestMapping(value = "/alunoInserir", method = RequestMethod.POST, consumes = "application/json")
public EntityAluno inserir(@RequestBody EntityAluno aluno, HttpSession sessao) {
repository.save(aluno);
return aluno;
}

使用此JSON:

{"login":"8","senha":"8","pessoa":{"nome":"asd","email":"sdf","telefone":"32"},"matricula":"22"}

有錯誤(在chrome的高級Rest Client中):

status: 400
error: "Bad Request"
exception: "org.springframework.http.converter.HttpMessageNotReadableException"
message: "Could not read JSON: Template must not be null or empty! (through reference chain: br.com.qrbibliokode.model.entities.EntityUsuario["pessoa"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: br.com.qrbibliokode.model.entities.EntityUsuario["pessoa"])"

該項目在這里: https : //github.com/felansu/Qr-BiblioKode-Ws

問題是什么? 謝謝

我解決了更新ModelPessoa實體的問題:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "tipo")
@JsonSubTypes({
        @Type(name = "aluno", value = EntityAluno.class),
        @Type(name = "funcionario", value = EntityFuncionario.class),
        @Type(name = "autor", value = EntityAutor.class)
})
public abstract class ModelPessoa extends UtilBaseEntities<Long> {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM