我正在开发一个用于登录和注册的 Spring Boot Rest API。我能够通过使用 postman 正文中的 @RequestBody 作为原始数据将数据保存在数据库中。但我想从 postman 发送每个字段的表单数据。在这种情况下,我应该在 Controller 中使用哪个注释。
如果有人有一个简单的例子,那就太好了!
请检查图片:
Dao 类 -
@Repository
public class CustomerDaoImpl implements CustomerDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Customer> getCustomers() {
Session session = sessionFactory.getCurrentSession();
Query<Customer> query = session.createQuery("from Customer",
Customer.class);
List<Customer> customers = query.getResultList();
return customers;
}
@Override
public Customer getCustomer(int id) {
Session session = sessionFactory.getCurrentSession();
Customer customer = session.get(Customer.class, id);
return customer;
}
@Override
public void saveCustomer(Customer theCustomer) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(theCustomer);
}
@Override
public void deleteCustomer(int Id) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("Delete from Customer where id=:customerId");
query.setParameter("customerId", Id);
query.executeUpdate();
}
@Override
public Customer getCustomer(String emailId, String password) {
Customer customer = null;
Session session = sessionFactory.getCurrentSession();
Query<Customer> query = session.createQuery("from Customer where emailId=:emailId and "
+ " password=:password");
query.setParameter("emailId", emailId);
query.setParameter("password", password);
List<Customer> list = query.getResultList();
try {
return customer = list.get(0);
}
catch(Exception e) {
e.getMessage();
return null;
}
}
}
POJO 类 -
import java.sql.Timestamp;
@Entity
@Table(name="tbl_user_register")
@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;
@Column(name="email_id")
private String emailId;
@Column(name="mobile")
private String mobile;
@Column(name="business_name")
private String businessName;
@Column(name="address")
private String address;
@Column(name="password")
private String password;
@Column(name="created")
@CreationTimestamp
private Timestamp created;
@Column(name="user_type")
private int userType;
@Column(name="parent_type")
private int parentType;
public Customer() {
super();
}
public Customer(String name, String emailId, String mobile, String businessName, String address, String password,
Timestamp created, int userType, int parentType) {
super();
this.name = name;
this.emailId = emailId;
this.mobile = mobile;
this.businessName = businessName;
this.address = address;
this.password = password;
this.created = created;
this.userType = userType;
this.parentType = parentType;
}
public Customer(int id, String name, String emailId, String mobile, String businessName, String address,
String password, Timestamp created, int userType, int parentType) {
super();
this.id = id;
this.name = name;
this.emailId = emailId;
this.mobile = mobile;
this.businessName = businessName;
this.address = address;
this.password = password;
this.created = created;
this.userType = userType;
this.parentType = parentType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Timestamp getCreated() {
return created;
}
public void setCreated(Timestamp created) {
this.created = created;
}
public int getUserType() {
return userType;
}
public void setUserType(int userType) {
this.userType = userType;
}
public int getParentType() {
return parentType;
}
public void setParentType(int parentType) {
this.parentType = parentType;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", emailId=" + emailId + ", mobile=" + mobile
+ ", businessName=" + businessName + ", address=" + address + ", password=" + password + ", created="
+ created + ", userType=" + userType + ", parentType=" + parentType + "]";
}
}
Controller -
@RestController
@RequestMapping("/api")
public class CustomerController {
@Autowired
CustomerService customerService;
ResponseHandler response;
@GetMapping("/customers")
public ResponseEntity<List<Customer>> getCustomers(){
List<Customer> list = customerService.getCustomers();
return new ResponseEntity<List<Customer>>(list, HttpStatus.OK);
}
@PostMapping(value="/login")
public Customer getCustomer(@RequestParam(value="emailId") String emailId, @RequestParam(value="password") String password) {
Customer theCustomer = customerService.getCustomer(emailId, password);
if(theCustomer == null) {
throw new CustomerNotFoundException("Customer not found - "+emailId);
}
return theCustomer;
}
@PostMapping(value = "/customers", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
public Customer saveCustomer(@ModelAttribute Customer customer) {
customerService.saveCustomer(customer);
return customer;
}
}
这就是我得到的 - /image/Nygrs.jpg
最佳答案
在 Controller 中,您可以getParameterMap()
,它将为您提供所有表单数据。
Map<String, String[]> parameterMap = request.getParameterMap();
或者只是将 map 作为参数
@RequestMapping("/yourmapping")
void method(@RequestParam HashMap<String, Object> formData){
//rest your code
}
关于java - 如何在 Spring Boot Rest 和 Postman 中使用表单数据来保存用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55842031/