我将JSON对象发送到Spring + Hibernate + MySQL应用程序。
Bellow是index.html
,您可以在其中查看发出请求的JavaScript。然后有Customer.java
实体和CustomerRestController.java
负责请求处理。不知怎的,我的数据库没有得到更新。
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btnBooking').click(function(){
var custName=$('#userName').val();
var custAddress=$('#address1').val();
var JSONObject= {"name":custName, "address":custAddress};
//var jsonData = JSON.stringify( JSONObject );
$.ajax({
type: "POST",
url: "http://192.168.1.12:8080/HomeServiceProvider/rest/customer/saveCustomer",
data: JSON.stringify( JSONObject ),
dataType: "json",
processData: true,
contentType: "application/json; charset=utf-8",
success: function(data, status, jqXHR){
alert("success..."+data);
},
error: function(xhr){
alert("error"+xhr.responseText);
}
});
});
});
</script>
</head>
<body>
<form class="form-horizontal" id="scheduleLaterForm" name="scheduleLaterForm" action="#" method="post">
<div class="col-lg-8">
<div class="form-group">
<label class="col-lg-3 control-label">Name:<font style="color: red;">*</font></label>
<div class="col-lg-9">
<input class="form-control" id="userName" name="userName" placeholder="Full Name" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Address:<font style="color: red;">*</font></label>
<div class="col-lg-9">
<input class="form-control" name="address1" id="address1" placeholder="Full address" value="" type="text">
</div>
</div>
<div class="form-group marg-bot-45">
<label class="col-lg-3 control-label"></label>
<div class="col-lg-9">
<button type="button" class="btn btn-info" id="btnBooking"> Confirm Booking </button>
</div>
</div>
</div>
</form>
</body>
</html>
Customer.java
@Entity
@Table(name="CUSTOMER")
public class Customer implements Serializable{
private static final long serialVersionUID = -82689141527424144L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column (name="CUST_ID")
private int custId;
@Column (name="NAME")
private String name;
@Column (name="ADDRESS")
private String address;
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
CustomerRestController.java
@RestController
@RequestMapping("/customer")
public class CustomerRestController {
private static Logger log = LogManager.getLogger(CustomerRestController.class);
@Value("${msg.customeradded}")
private String message;
@Value("${msg.successcode}")
private int code;
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/saveCustomer", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status saveCustomer(@RequestBody Customer customer){
try {
customerService.saveCustomer(customer);
return new Status(code, message);
} catch (Exception e) {
return new Status(0, e.toString());
}
}
@RequestMapping(value="/getAllCustomer",method=RequestMethod.GET, headers="Accept=application/json")
public @ResponseBody List<Customer> getAllCustomer(){
List<Customer> customers = null;
try {
customers = customerService.getAllCustomer();
log.info("Size:"+customers.size());
log.info("customers:"+customers);
} catch(Exception e) {
e.printStackTrace();
}
return customers;
}
}
CustomerService.java
public interface CustomerService {
public void saveCustomer(Customer customer);
public List<Customer> getAllCustomer();
}
CustomerServiceImpl.java
@Service
public class CustomerServiceImpl implements CustomerService {
private static Logger log = LogManager.getLogger(CustomerServiceImpl.class);
@Autowired
private CustomerDao customerDao;
public void saveCustomer(Customer customer) {
log.info("customer first name:" + customer.getCustName());
customerDao.saveCustomer(customer);
}
public List<Customer> getAllCustomer() {
List<Customer> customers = customerDao.getAllCustomer();
log.info("Size:"+customers.size());
log.info("customers:"+customers);
return customers;
}
}
Name of My table is CUSTOMER and columns are
colomn1:CUST_ID
colomn2:NAME
colomn3:ADDRESS