Spring REST Controller返回空JSON。可重复的数据结构。为什么?

问题描述 投票:1回答:2

我意识到一个非常相似的问题被提出并被关闭,因为它不够具体而且没有指明结果。 Closed Off Topic

问题:从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>[email protected]</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

@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 import Omitted

@Repository
public interface CategoryCRUD  extends CrudRepository<Category, Long> {
  List<Category> findByTitle(String title);
}

休息控制器

//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; 
  }


}
json spring rest spring-boot
2个回答
1
投票

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

实体

//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;
  }

}

欢迎更好的答案。


0
投票

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

您可以使用来自lombok的@Getter, @Setter, @AllArgsConstructor, @NoArgsConstructor javadoc注释来为您的字段生成getter和setter以及构造函数。

有关更多信息,请查看此http://www.baeldung.com/intro-to-project-lombok

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.