繁体   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