测试使用Postman和Curl发送Spring Boot后端的Post请求

huangapple go评论82阅读模式
英文:

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.
测试使用Postman和Curl发送Spring Boot后端的Post请求

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 = &quot;pharmaceuticals&quot;)
public class Pharmaceutical {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(name = &quot;genericName&quot;)
	private String genericName;
	
	@Column(name = &quot;brandNames&quot;)
	private ArrayList&lt;String&gt; brandNames;
	
	@Column(name = &quot;strength&quot; )
	private String strength;
	
	@Column(name = &quot;quantity&quot;)
	private Integer quantity; 
	
	@ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.MERGE,
                CascadeType.REFRESH
            })
	
	@JoinTable(name = &quot;pharm_commonuses&quot;,
		joinColumns = { @JoinColumn(name = &quot;pharmaceutical_id&quot;) },
		inverseJoinColumns = { @JoinColumn(name = &quot;commonUse_id&quot;) })
	private Set&lt;CommonUse&gt; commonUses = new HashSet&lt;&gt;();
	
	public Pharmaceutical() {}
		
	public Pharmaceutical(String genericName, ArrayList&lt;String&gt; 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 = &quot;http://localhost:8081&quot;)
@RestController
@RequestMapping(&quot;/api&quot;)
public class PharmaceuticalController {
	
	@Autowired
	PharmaceuticalRepository pharmRepository;
	CommonUseRepository comRepository;
	
	@GetMapping(&quot;/pharmaceuticals&quot;)
	public ResponseEntity&lt;List&lt;Pharmaceutical&gt;&gt; getPharmaceuticals(@RequestParam(required = false) String title){
		List&lt;Pharmaceutical&gt; pharms = new ArrayList&lt;Pharmaceutical&gt;();
		pharmRepository.findAll().forEach(pharms::add);
		return new ResponseEntity&lt;&gt;(pharms, HttpStatus.OK);
	} 
	
	@PostMapping(&quot;/pharmaceuticals&quot;)
	public ResponseEntity&lt;Pharmaceutical&gt; createPharmaceutical(@RequestBody String generic, ArrayList&lt;String&gt; brands, String strength, Integer quant, ArrayList&lt;String&gt; 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&lt;&gt;(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

huangapple
  • 本文由 发表于 2020年8月20日 04:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63494339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定