简体   繁体   中英

Testing Spring Boot Backend Post Request using Postman and Curl

I've been trying to test some simple GET and POST request methods, using Postman and curl through command line.

For some reason, when I try to create a json file and send it through Postman, it saves all the data into the first variable.

I have no idea what's going on. The frontend will deliver everything through JSON files, so if this isn't working, then I want to fix it before finishing up my controller.

Here's my pharmaceutical model:

@Entity
@Table(name = "pharmaceuticals")
public class Pharmaceutical {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @Column(name = "genericName")
    private String genericName;
    
    @Column(name = "brandNames")
    private ArrayList<String> brandNames;
    
    @Column(name = "strength" )
    private String strength;
    
    @Column(name = "quantity")
    private Integer quantity; 
    
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.MERGE,
                CascadeType.REFRESH
            })
    
    @JoinTable(name = "pharm_commonuses",
        joinColumns = { @JoinColumn(name = "pharmaceutical_id") },
        inverseJoinColumns = { @JoinColumn(name = "commonUse_id") })
    private Set<CommonUse> commonUses = new HashSet<>();
    
    public Pharmaceutical() {}
        
    public Pharmaceutical(String genericName, ArrayList<String> brandNames, String strength,
            Integer quantity) {
        this.genericName = genericName;
        this.brandNames = brandNames;
        this.strength = strength;
        this.quantity = quantity;
    }
    //getters and setters

Here's my controller:

@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api")
public class PharmaceuticalController {
    
    @Autowired
    PharmaceuticalRepository pharmRepository;
    CommonUseRepository comRepository;
    
    @GetMapping("/pharmaceuticals")
    public ResponseEntity<List<Pharmaceutical>> getPharmaceuticals(@RequestParam(required = false) String title){
        List<Pharmaceutical> pharms = new ArrayList<Pharmaceutical>();
        pharmRepository.findAll().forEach(pharms::add);
        return new ResponseEntity<>(pharms, HttpStatus.OK);
    } 
    
    @PostMapping("/pharmaceuticals")
    public ResponseEntity<Pharmaceutical> createPharmaceutical(@RequestBody String generic, ArrayList<String> brands, String strength, Integer quant, ArrayList<String> common){
        Pharmaceutical newPharm = new Pharmaceutical(generic, brands, strength, quant);
        for (String name: common) {
            CommonUse com = new CommonUse(name);
            comRepository.save(com);
            newPharm.getCommonUses().add(com);
        }
        pharmRepository.save(newPharm);
        return new ResponseEntity<>(newPharm, HttpStatus.CREATED);
    }
}

Any help would be great!

This is your problem:

@RequestBody String generic

You are saying that the body that comes in, should be placed into this string.

You should build an object representation of the body you are sending in and change it to:

@RequestBody PharmaceuticalRequest generic

and then remove all the other input parameters in the createPharmaceutical function.

Reference: https://www.baeldung.com/spring-request-response-body#@requestbody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM