0

I return an array of Java Instances in my Spring-Boot-Get-Started project.

package com.wepay.business.resource;

import com.wepay.business.model.Good;
import com.wepay.business.repo.GoodRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:9000", "http://localhost:8083"})
@RestController
@RequestMapping("/api")
public class GoodResource {
    @Autowired
    GoodRepository repository;

    @GetMapping("/getGood")
    public List<Good> getAllGoods() {
        List<Good> goods = new ArrayList<>();
        repository.findAll().forEach(goods::add);
        return goods;
    }
}
package com.wepay.business.repo;

import com.wepay.business.model.Good;
import org.springframework.data.repository.CrudRepository;

public interface GoodRepository extends CrudRepository<Good, Long> {

}
package com.wepay.business.model;

import javax.persistence.*;


@Entity
@Table(name = "good")
public class Good {

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

  @Column(name = "name")
  private String name;

  @Column(name = "price")
  private double price;

  @Column(name = "img")
  private String img;

  @Column(name = "info")
  private String info;

  @Column(name = "amount")
  private int amount;

  @Column(name = "address")
  private  String address;

  @Column(name = "soldAmount")
  private String soldAmount;

  @Column(name = "sellerId")
  private String sellerId;

  public Good(){

  }

  public Good(String name, Double price, String info, int amount) {
    this.name = name;
    this.price = price;
    this.info = info;
    this.amount = amount;
  }

  public Good(Long id, String goodName, Double unitPrice, String goodInfo, int amount) {
      this(goodName, unitPrice, goodInfo, amount);
      this.id = id;
  }

  public void setId(Long id) {
    this.id = id;
  }
}

The value of goods is an array of Java Instacnes enter image description here But there is only an empty array in the http response body.

enter image description here

I guess that I should return an array of JSON objects rather than Java Instances.

Do I need to convert the Java Instances to JSON objects? If so, is there any framework to help us to do this job?

I have been blocked by this issue since last week. Thanks in advance.

3
  • show this class Good and make sure it has getters and setters Commented Jan 10, 2020 at 12:31
  • class Good is added @Deadpool Commented Jan 10, 2020 at 12:38
  • @Deadpool You're right. I should add getters Commented Jan 10, 2020 at 12:43

3 Answers 3

3

The problem resides in the fact that your Good class has no getters (atleast by what I see in your post). Add the getters and this should work.

I think you can use JpaRepository<T, ID> instead of CrudRepository<T, ID> so in this case there's no need to instantiate another List<Good>, because the repository.findAll() already returns List<Good> inside the JpaRepository, although by the way you're doing, it should also work normally.

Do I need to convert the Java Instances to JSON objects? If so, is there any framework to help us to do this job?

No. Spring already do it for you by using Jackson's serializer.

Sign up to request clarification or add additional context in comments.

3 Comments

It seems that I need to add getters for every item. Is there any mechanism to automate this process?
The IDE can generate the getters and setters for you. In IntelliJ, press ctrl + enter and select the option Getters and Setters. In Eclipse, press ctrl + 3, type 'Generate' and see the options. One of them if 'Generate Getters and Setters'. Also, take a look at the project lombok. You can use annotations that will generate getters and setters when the java class is compiled.
Also, for those who don't know: the serializer, and the deserializer will use getters and setters to retrieve the data from the field and set the data to the field, respectively. So, when receiving a JSON payload as request, you must have setters in order to get the deserializer working to create the objects
0

try return repository.findAll();

11 Comments

After I try return repository.findAll(), it seems that the return type of this function needs to be updated?
if repository.findAll() return List<Good> that you don't need update anything.
repository.findAll() returns java.lang.Iterable<Good> while the type of List<good> is java.util.List<Good>. They cannot be converted each other.
I updated the return type to Iterable<Good>. But it still returns an empty array in the http response body.
I updated my GoodRepository interface in the question.
|
0

If there is no specific reason to use CrudRepository you can change it to JpaRepository

By doing this you can avoid conversion of Iterator to List and use like this.

public interface GoodRepository extends JpaRepository<Good, Long> {

}

// Controller
@GetMapping("/getGood")
public List<Good> getAllGoods() {
    return repository.findAll();
}

Also, Make sure Getter Setter is in place for each persistable field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.