title: How to implement API returns nested JSON with OneToMany relationship in Spring Boot?
body: <p>I am developing simple API for practice project Online Shopping System. I am totally new in Spring Boot framework and creating API. </p>
  
  <p>I want to return JSON similar to this: </p>
  
  <pre><code>[
  {
      "id": 1,
      "name": "pname_46",
      "description": "pdesc_793_793_793_79",
      "price": 519.95,
      "details": [{"orderId": 10,
                   "productId": 1,
                   "quantity": 4
                  }
                  {"orderId": 12,
                   "productId": 1,
                   "quantity": 5
                  }]
  },
  {
      "id": 2,
      "name": "pname_608",
      "description": "pdesc_874_874_874",
      "price": 221.7,
      "details": [{"orderId": 20,
                   "productId": 2,
                   "quantity": 2
                  }
                  {"orderId": 3,
                   "productId": 2,
                   "quantity": 67
                  }]
  }]
  </code></pre>
  
  <p>Here is my <code>@Entity</code> classes:</p>
  
  <p>Product.java</p>
  
  <pre><code>@Entity
  @Table(name = "Products")
  public class Product implements Serializable {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "pcod")
  private int id;
  
  @Column(name = "pnam")
  private String name;
  
  @Column(name = "pdes")
  private String description;
  
  @Column(name = "price")
  private Double price;
  
  @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
  private List&lt;Detail&gt; details = new ArrayList&lt;&gt;();
  
  //Constructor, setter, and getter ..
  
  }
  </code></pre>
  
  <p>Detail.java</p>
  
  <pre><code>@Entity
  @Table(name = "Details")
  public class Detail {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ordid")
  private Order order;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "pcod")
  private Product product;
  
  @Column(name = "qty")
  private int quantity;
  
  //constructor, setters, and getters ..  
  }
  </code></pre>
  
  <p>There is also class named Order.java similar to Product.java</p>
  
  <p>ProductRepository.java</p>
  
  <pre><code>@Repository
  public interface ProductRepository extends JpaRepository&lt;Product, Integer&gt; {
  
  }
  </code></pre>
  
  <p>OnlineShoppingApiController.java</p>
  
  <pre><code>@RestController
  public class OnlineShoppingApiController {
  @Autowired
  ProductRepository productRepository;
  
  @GetMapping("/products")
  public List&lt;Product&gt; getAllProducts(){
      return productRepository.findAll();
  }
  
  @GetMapping("/products/id={id}")
  public Optional&lt;Product&gt; getOneProduct(@PathVariable String id){
      int pid = Integer.parseInt(id);
      return productRepository.findById(pid);
  }
  }
  </code></pre>
  
  <p>ProjectApplication.java</p>
  
  <pre><code>@SpringBootApplication
  public class ProjectApplication {
  
  public static void main(String[] args) {
      SpringApplication.run(ProjectApplication.class, args);
  }
  }
  </code></pre>
  
  <p>This program gets data from MySql database. There are stored data in tables. </p>
  
  <p>Tables look like this:<br>
  <strong>Products:</strong><br>
   <em>- pcod<br>
   - pnam<br>
   - pdes<br>
   - price</em>  </p>
  
  <p><strong>Details:</strong><br>
   <em>- ordid<br>
   - pcod<br>
   - qty</em>  </p>
  
  <p>Here is my pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;com.example&lt;/groupId&gt;
  &lt;artifactId&gt;project&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;name&gt;project&lt;/name&gt;
  &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I run the application and check the API using POSTMAN, I am getting this result:</p>
  
  <pre><code>{
  "timestamp": "2018-04-04T13:39:44.021+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]-&gt;com.example.project.pojo.Product[\"details\"])",
  "path": "/products"
  </code></pre>
  
  <p>}</p>
  
  <p>How can I solve this problem? </p>
  
  <p>Thanks for the answer</p>