SpringREST控制器返回空JSON。可扩展的数据结构。为什么?

SpringREST控制器返回空JSON。可扩展的数据结构。为什么?,json,spring,rest,spring-boot,Json,Spring,Rest,Spring Boot,我意识到有一个非常类似的问题被问到了,然后就结束了,因为它不够具体,也没有具体说明结果 问题:从REST控制器返回的JSON为空。已验证的数据存在,并且在Iterable中 预期结果:将返回包含对象的JSON数组 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

我意识到有一个非常类似的问题被问到了,然后就结束了,因为它不够具体,也没有具体说明结果

问题:从REST控制器返回的JSON为空。已验证的数据存在,并且在Iterable中

预期结果:将返回包含对象的JSON数组

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.codeexcursion</groupId>
  <organization>
    <name>Chris Lynch</name>
  </organization>
  <version>1.00.000</version>
  <artifactId>work-tracking</artifactId>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>


  <name>Work Tracking</name>

  <inceptionYear>2017</inceptionYear>

  <developers>
    <developer>
      <id />
      <name>Chris Lynch</name>
      <email>chrislynch42@yahoo.com</email>
      <timezone>-4</timezone>
      <roles>
        <role>Chief cook and bottle washer.</role>
      </roles>
    </developer>
  </developers>

  <dependencies>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.10.19</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.10.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <version>1.5.10.RELEASE</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>1.5.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-rest</artifactId>
      <version>1.5.10.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>10.13.1.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>com.codeexcursion.Application</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>
资源

//Package and import Omitted

@Repository
public interface CategoryCRUD  extends CrudRepository<Category, Long> {
  List<Category> findByTitle(String title);
}
//省略了打包和导入
@存储库
公共接口类别CRUD扩展了CrudRepository{
列表FindBytle(字符串标题);
}
休息控制器

//Package and import Omitted

@RestController
@RequestMapping("/categories")
public class CategoryController {

  @Autowired
  private CategoryCRUD categoryCRUD;  


  @RequestMapping(value = "", method = RequestMethod.GET)
  public @ResponseBody Iterable<Category> list() {
    System.out.println("findAll");
    categoryCRUD.findAll().forEach((category) -> {
      System.out.println("category=" + category);
    });    
    return categoryCRUD.findAll();
  }

  private List<Category> findAll() {
    final Iterable<Category> data = categoryCRUD.findAll();
    List<Category> returnList = new ArrayList<>();
    data.forEach(returnList::add);
    return returnList; 
  }


}
//省略了打包和导入
@RestController
@请求映射(“/categories”)
公共类类别控制器{
@自动连线
私有类别CRUD CategoryCRUD;
@RequestMapping(value=”“,method=RequestMethod.GET)
public@ResponseBody可分配列表(){
System.out.println(“findAll”);
categoryCRUD.findAll().forEach((类别)->{
System.out.println(“category=“+category”);
});    
返回categoryCRUD.findAll();
}
私有列表findAll(){
final Iterable data=categoryCRUD.findAll();
List returnList=new ArrayList();
data.forEach(returnList::add);
退货清单;
}
}

我找到了一个答案,这个答案在关闭的帖子中有所暗示,但没有详细说明。我需要向实体中添加getter。我希望JPA/Spring能够自动添加getter和setter。下面的问题解决了

实体

//Package and import Omitted

@Entity
public class Category {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private Long parentId;

  private String title;

  private String description;

  protected Category() {
  }

  public Category(final String title, String description) {
    this(0L, title, description);
  }

  public Category(Long parentId, final String title, String description) {
    if (parentId == null) {
      parentId = 0L;
    }
    if (title == null || title.trim().isEmpty()) {
      throw new IllegalArgumentException("Title may not be null or empty.");
    }
    if (description == null) {
      description = "";
    }
    this.parentId = parentId;
    this.title = title;
    this.description = description;
  }

  @Override
  public String toString() {
    return "id = " + id + "; parentId=" + parentId + "; title=" + title + "; description=" + description;
  }


}
//Package and imports omitted
@Entity
public class Category {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private Long parentId;

  private String title;

  private String description;

  protected Category() {
  }

  public Category(final String title, String description) {
    this(0L, title, description);
  }

  public Category(Long parentId, final String title, String description) {
    if (parentId == null) {
      parentId = 0L;
    }
    if (title == null || title.trim().isEmpty()) {
      throw new IllegalArgumentException("Title may not be null or empty.");
    }
    if (description == null) {
      description = "";
    }
    this.parentId = parentId;
    this.title = title;
    this.description = description;
  }

  @Override
  public String toString() {
    return "id = " + id + "; parentId=" + parentId + "; title=" + title + "; description=" + description;
  }

  public Long getId() {
    return id;
  }

  public Long getParentId() {
    return parentId;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }

}

欢迎提供更好的答案。

您必须在
pom.xml
文件中包含
lombok
依赖项,并且必须在您使用的IDE中设置
lombok
jar(可以是Intellij或Eclipse)。如果您想使用注解
@Data
,它会在Java Bean或Pojo类中自动生成getter、setter和toString()方法

您可以使用
@Getter、@Setter、@allargsconstuctor、@noargsconstuctor
来自lombok的javadoc注释将为您的字段生成Getter、Setter和构造函数

更多信息请看这个

谢谢

在我的例子中,实体字段的getter不是公共的


将它们公开为我解决了这个问题。

如果您希望自动生成getter/setter,您可能需要看看projectlombok:Mate,非常感谢!工作得很有魅力。只是在我的模型中添加了getter,这就成功了。真傻,竟然忘了!Lombok不是Spring的一部分,而是提供getter和setter注释的第三方库,我的理解正确吗?是的,它是第三方依赖,主要用于许多项目。如果您使用的是SpringBoot,那么可以将其包含在(SpringProject初始值设定项)中。是的,它不是Spring框架的一部分。你救了我一天。我忘了在
实体
类中添加
getter