我有一些表单数据,我正在使用Spring Boot,并且正在通过AJAX发送POST请求,并且已成功将其插入数据库中。但是,由于已将其插入数据库中,因此我希望得到响应,但是 console.log(响应)中没有响应。
由于我的ajax代码是,表单数据已成功插入数据库中
var employee={
"iNumber":$("#iNumber").val(),
"fullName":$("#fullName").val(),
"joinedDate":$("#joinedDate").val(),
"position":$("#position").val(),
"reportsTo":$("#reportsTo").val(),
"cubicleNo":$("#cubicleNo").val(),
"jobType":$("#jobType").val()
};
$.ajax({
url: A_PAGE_CONTEXT_PATH + "/insert-emp",
method: "post",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(employee),
success: function(response) {
console.log(response);
}, error: function(response){
switch(response.status){
case 409:
alert("error");
}
}
});
其余的Api是:
package com.ashwin.vemployee.restcontroller;
import com.ashwin.vemployee.model.Employee;
import com.ashwin.vemployee.response.ResponseMessage;
import com.ashwin.vemployee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.validation.Valid;
import java.net.URI;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService empService;
@PostMapping("/insert-emp")
public ResponseEntity<Object> createUser(@Valid @RequestBody Employee employee){
Employee savedUser= empService.saveEmployee(employee);
//return status of HTtp after created
URI location= ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").
buildAndExpand(savedUser.getiNumber()).toUri();
return ResponseEntity.created(location).build();
}
}
服务类别为:
package com.ashwin.vemployee.service;
import com.ashwin.vemployee.model.Employee;
import com.ashwin.vemployee.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository empRepository;
public Employee saveEmployee(Employee employee){
if(employee.getiNumber()==0){
empRepository.save(employee);
}
else{
empRepository.save(employee);
}
return employee;
}
}
存储库为:
package com.ashwin.vemployee.repository;
import com.ashwin.vemployee.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
}
数据已成功插入数据库中,但响应没有任何反应,进入了我的console.log(response).
我的pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ashwin</groupId>
<artifactId>vemployee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vemployee</name>
<description>Demo project for Spring Boot for offc</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- needed for jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.27</version>
</dependency>
<!--bootsrap and jquery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap-datepicker -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.7.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.oracle.ojdbc</groupId>-->
<!-- <artifactId>ojdbc8</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的模型类Employee.java是:
package com.ashwin.vemployee.model;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer iNumber;
@NotBlank
private String fullName;
// @NotBlank
private String joinedDate;
@NotBlank
private String position;
@NotBlank
private String reportsTo;
@NotBlank
private String cubicleNo;
@NotBlank
private String jobType;
public Employee() {
}
public Integer getiNumber() {
return iNumber;
}
public void setiNumber(Integer iNumber) {
this.iNumber = iNumber;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getJoinedDate() {
return joinedDate;
}
public void setJoinedDate(String joinedDate) {
this.joinedDate = joinedDate;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getReportsTo() {
return reportsTo;
}
public void setReportsTo(String reportsTo) {
this.reportsTo = reportsTo;
}
public String getCubicleNo() {
return cubicleNo;
}
public void setCubicleNo(String cubicleNo) {
this.cubicleNo = cubicleNo;
}
public String getJobType() {
return jobType;
}
public void setJobType(String jobType) {
this.jobType = jobType;
}
}
答案 0 :(得分:0)
您没有在响应正文中设置任何内容,可以这样设置-
ResponseEntity.created(location).body(savedUser).build()