0

i am developing an API application, however I am having difficulties with a particular method.

The getCertificationPOJO method, is responsible for taking in a Store as a parameter and searching if that store exists in the database.

Now the problem here is how do I pass a Store object as a parameter in Postman. I have tried passing it in as a JSON string, but that is not working.

Apologies for the bad edit

Certification Controller

@Controller
public class CertController {

    @Autowired
    private CertificationRepository certRepo;
    @Autowired
    private StoreRepository StoreRepository;

    
    @GetMapping(value = "/getCertObject")
    public @ResponseBody
    Optional<Certification> getCertificationPOJO(@RequestParam Store store)
    {return Lists.newArrayList(certRepo.findAll()).stream().filter(e->e.getStore() == store).findFirst();}

}

Store Class

@Entity
@Table(name = "store")
public class Store implements com.halal.abstractions.Entity {

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

    @JsonIgnore
    @OneToOne(optional = true) // By default this is set to true so technically this is redundant but, yea lets
                               // just keep it there
    private Certification certification;

    @NotNull
    @Column(nullable = false)
    private String name;

    private String phoneNumber;

    @NotNull
    @Column(nullable = false)
    private String address;

    @NotNull
    @Column(nullable = false)
    private double latitude;

    @NotNull
    @Column(nullable = false)
    private double longitude;

    @NotNull
    @Column(nullable = false)
    private Date dateAdded;

    static final DateFormat DF = new SimpleDateFormat("dd/MM/yyyy");

    protected Store() {
    }

    public Store(String name, String phoneNumber, String address, double latitude, double longitude) {
        this.name = name;
        this.setPhoneNumber(phoneNumber);
        this.setAddress(address);
        this.latitude = latitude;
        this.longitude = longitude;
        this.dateAdded = new Date(System.currentTimeMillis());
    }
    @Override
    public Long getId() {
        return this.id;
    }
    @Override
    public void setId(long id) {
        this.id = id;
    }
    @Override
    public String getName() {
        return name;
    }
    @Override
    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    @Override
    public String getDateAdded() {
        return DF.format(dateAdded);
    }

    @Override
    public void setCertification(Certification certification) {
        this.certification = certification;
    }
    @Override
    public Certification getCertification() {
        return this.certification;
    }

    @Override
    public String toString() {
        return "Store{" + "id=" + id + ", certification=" + certification + ", name='" + name + '\'' + ", phoneNumber='"
                + phoneNumber + '\'' + ", address='" + address + '\'' + ", latitude=" + latitude + ", longitude="
                + longitude + ", dateAdded=" + dateAdded + '}';
    }
}

Certification Class

@Entity
@Table(name = "certification")
public class Certification {

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

    @NotNull
    @Column(nullable = false)
    private boolean isCertified;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    @JsonIgnore
    private Store store;

    public Certification() {
    }

    public long getId() {
        return this.id;
    }

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

    public Store getStore() {
        return this.store;
    }

    public void setStore(Store store) {
        this.store = store;
    }

    public boolean isIsCertified() {
        return this.isCertified;
    }

    public void setIsCertified(boolean isCertified) {
        this.isCertified = isCertified;
    }

    @Override
    public String toString() {
        return "Certification{" + "id=" + id + ", isCertified= " + isCertified + '}';
    }
}

Postman Screenshot

Hakan Dilek
  • 2,024
  • 2
  • 21
  • 30

3 Answers3

1

I would advise against sending a body in a GET request, see similar answer here

The query param route that @Murilo suggests is one way but if the way of determining whether a getCertificationPOJO already exists in the database depends solely on the id then you may only need to send the id in which case a path variable would be best like below where {id} can be replaced with the actual ID

GET /getCertObject/{id}

and then in the controller

@GetMapping("/getCertObject/{id}")
@ResponseBody
Optional<Certification> getCertificationPOJO(@PathVariable String id) {
  ...
}
Joseph Berry
  • 148
  • 8
0

You don't use Params section in this case. You use Body. Click on that tab and put your JSON OBJECT in the textbox that will appear. You also need to set CONTENT TYPE to JSON by selecting the proper value in the COMBO that appears farthest to the right of the attached image.

Check this out:

enter image description here

Pablo Santa Cruz
  • 162,219
  • 30
  • 224
  • 277
0

If you are using queryParams you have to pass individually each key/param, in your Postman you fill like it:

key: id          value: 1
key: name        value "asdasd"
key: phoneNumber value: "000"

(...)

Another option is you change your strategy and pass the entire json in your Body, but you will have to change your RestController to receive a @RequestBody instead of @RequestParam

Murilo Góes de Almeida
  • 1,157
  • 1
  • 12
  • 33