是否可以在结束Post方法之前从POST正文中提取和使用值?
POST正文:
{
"proposalId": 124,
"type": "credit",
"customerId": "1001"
}
我想检查数据库中是否已存在customerId,是否仅存在以保存实体。
/*---Add new proposal---*/
@PostMapping(value="/proposal", produces = "application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity<?> saveProposal(HttpServletRequest request,
HttpServletResponse response,@RequestBody Proposal proposal ) {
String jsonString = request.getParameter("customerId");
System.out.println(jsonString);
// Long jsonLong = Long.valueOf(jsonString);
long id = 0;
//Customer checkCustomer = customerService.showCustomer(jsonLong);
// System.out.println(checkCustomer);
// if(checkCustomer!=null)
//id = proposalService.save(proposal);
return ResponseEntity.ok().body("New Proposal has been saved with ID:" + id);
}
我尝试了不同的方法,但我总是得到一个空值。 任何消化都会很好,当然如果我能以更时尚的方式写这个,我会很高兴学习。 谢谢!
编辑:
提案类:
package model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name= "PROPOSAL")
@JsonIgnoreProperties(ignoreUnknown = false)
public class Proposal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long proposalId;
private String type;
@ManyToOne(optional = false, cascade = CascadeType.MERGE, fetch=FetchType.EAGER)
@JoinColumn(name="customerId")
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Long getProposalId() {
return proposalId;
}
public void setProposalId(Long proposalId) {
this.proposalId = proposalId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Proposal [proposalId=" + proposalId + ", type=" + type + ", customer=" + customer + "]";
}
}
客户类
package model;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name= "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long customerId;
private String heading;
private int ndg;
private Date birthdate;
private String name;
private String surname;
// @OneToMany(mappedBy="customer", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
/* private List<Proposal> proposal;
public List<Proposal> getProposal() {
return proposal;
}
public void setProposal(List<Proposal> proposal) {
this.proposal = proposal;
}*/
public Long getUserId() {
return customerId;
}
public void setUserId(Long customerId) {
this.customerId = customerId;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public int getNdg() {
return ndg;
}
public void setNdg(int ndg) {
this.ndg = ndg;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
答案 0 :(得分:0)
当你已经在porposal对象中拥有所需的一切时,你不需要手动解析身体。 @RequestBody
注释将请求主体绑定到参数。所以你可以使用以下代码:
@PostMapping(value="/proposal", produces = "application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity<?> saveProposal(HttpServletRequest request,
HttpServletResponse response,@RequestBody Proposal proposal ) {
long customerId = proposal.getCustomer().getUserId();
Customer checkCustomer = customerService.showCustomer(customerId );
if(checkCustomer!=null) {
proposalService.save(proposal);
return ResponseEntity.ok().body("New Proposal has been saved with ID:" + proposal.getProposalId());
} else {
return ResponseEntity.ok(); //change return to your liking
}
}
编辑:Json不代表Porposal结构。提案中的客户是一个对象,因此它必须是Json中的一个对象。
{
"proposalId": 124,
"type": "credit",
"customer": {
"customerId": "1001",
"name": "Jon",
"surname": "Do"
}
}