在JSON响应中获取映射表的其他字段的空值

时间:2019-08-26 15:41:50

标签: json spring spring-boot spring-restcontroller

在JSON响应中获取空值。

模型类:

    @Data
    @Entity
    @Table(name = "TABLE_NAME")
    @AllArgsConstructor
    @NoArgsConstructor
    public class Model{

    @EmbeddedId
    private MedelId id;

    @Embeddable
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MedelId implements Serializable {
     static final long serialVersionUID = 1L;

        @Column(name = "first")
        private String first;
        @Column(name = "last")
        private String last;
    }
 }

DAO界面:

public interface ModelRepository extends CrudRepository<Model, Model.ModelId> 
 {}

控制器类:

      @RestController
      @ConditionalOnProperty(value = "api.enabled", havingValue = "true")
      @RequestMapping(value = "/api/v1/model", produces = "application/json")
      @AllArgsConstructor
      public class MyController {

    private ModelRepository repo;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Iterable<Model> list() {
        return repo.findAll();
    }
}

结果:

[null,null,null,null,null,null,null,null,null,{"id":{"first":"X","last":"value"}},null]

如果列值之一在表中为空,我将得到空值。

即使一列为空,请帮助我如何获取所有对象

0 个答案:

没有答案