我正在开发用于登录和注册的Spring Boot Rest API。通过使用邮递员的正文中的@RequestBody作为原始数据,我能够将数据保存在数据库中。但是我想从每个领域的邮递员那里发送表格数据。在这种情况下,我应该在控制器中使用哪个注释。
如果有人举一个简单的例子,那就太好了!
请检查图像:
道课-
@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 + "]";
}
}
控制器-
@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;
}
}
这就是我得到的-https://imgur.com/RnMkztk
答案 0 :(得分:0)
在控制器中,您可以getParameterMap()
来获取所有表单数据。
Map<String, String[]> parameterMap = request.getParameterMap();
或仅将地图作为参数
@RequestMapping("/yourmapping")
void method(@RequestParam HashMap<String, Object> formData){
//rest your code
}
答案 1 :(得分:0)
您可以在consumes = MediaType.MULTIPART_FORM_DATA_VALUE
中提供@PostMapping
。 DataRequest
是一个模型,它将包括名称,电子邮件等数据字段。并且还将在DataRequest中添加getter设置方法。
@PostMapping(value = "/readFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String readFile (@ModelAttribute DataRequest dataRequest) {
//DataRequest is the POJO in which you can mention the fields you want in the request
return "done";
}
@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataRequest {
private String _id;
private Date createTime;
//add getter setters
}