英文:
Testing Spring Boot Backend Post Request using Postman and Curl
问题
我一直在尝试测试一些简单的GET和POST请求方法,使用Postman和通过命令行使用curl。
由于某种原因,当我尝试创建一个JSON文件并通过Postman发送它时,它会将所有数据保存在第一个变量中。
我不知道发生了什么。前端将通过JSON文件传递所有内容,所以如果这不起作用,我想在完成控制器之前解决这个问题。
这是我的药物模型:
@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);
}
}
任何帮助都将是极好的!
英文:
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!
答案1
得分: 0
这是你的问题:
@RequestBody String generic
你在说传入的请求体应该放入这个字符串中。
你应该构建一个请求体的对象表示,并将其改为:
@RequestBody PharmaceuticalRequest generic
然后,在createPharmaceutical
函数中移除所有其他的输入参数。
参考链接:
https://www.baeldung.com/spring-request-response-body#@requestbody
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论