Java 使用Postman和Curl测试Spring引导后端Post请求
我一直在尝试测试一些简单的GET和POST请求方法,通过命令行使用Postman和curl 出于某种原因,当我尝试创建一个json文件并通过Postman发送它时,它会将所有数据保存到第一个变量中。 我不知道发生了什么事。前端将通过JSON文件交付所有内容,因此如果这不起作用,那么我希望在完成控制器之前修复它 这是我的药物模型:Java 使用Postman和Curl测试Spring引导后端Post请求,java,spring,spring-boot,request,postman,Java,Spring,Spring Boot,Request,Postman,我一直在尝试测试一些简单的GET和POST请求方法,通过命令行使用Postman和curl 出于某种原因,当我尝试创建一个json文件并通过Postman发送它时,它会将所有数据保存到第一个变量中。 我不知道发生了什么事。前端将通过JSON文件交付所有内容,因此如果这不起作用,那么我希望在完成控制器之前修复它 这是我的药物模型: @Entity @Table(name = "pharmaceuticals") public class Pharmaceutical {
@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
这是我的控制器:
@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
你的意思是,进来的身体,应该放在这根绳子里
您应该为要发送的实体构建对象表示,并将其更改为:
@RequestBody PharmaceuticalRequest generic
然后删除createPharmaceutical函数中的所有其他输入参数
参考: