繁体   English   中英

使用 Postman 和 Curl 测试 Spring 引导后端发布请求

[英]Testing Spring Boot Backend Post Request using Postman and Curl

我一直在尝试通过命令行使用 Postman 和 curl 来测试一些简单的 GET 和 POST 请求方法。

出于某种原因,当我尝试创建一个 json 文件并通过 Postman 发送它时,它会将所有数据保存到第一个变量中。

我不知道是怎么回事。 前端将通过 JSON 文件提供所有内容,所以如果这不起作用,那么我想在完成我的 controller 之前修复它。

这是我的药品 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

这是我的 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);
    }
}

任何帮助都会很棒!

这是你的问题:

@RequestBody String generic

你是说进来的身体应该放在这个字符串中。

您应该构建一个 object 表示您要发送的正文并将其更改为:

@RequestBody PharmaceuticalRequest generic

然后删除createPharmaceutical function 中的所有其他输入参数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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