2017-04-05 72 views
2

我是Spring MVC的新手,我正在构建一个With angularjs和Spring Boot。Angularjs发布弹簧启动电话

当我试图通过angularjs进行POST调用时,出现此错误。 但是,当我尝试与邮递员它正在与x-www-form-urlencoded,但与表单数据我得到下面的错误。

邮递员错误

{ 
 
    "timestamp": 1491406541851, 
 
    "status": 500, 
 
    "error": "Internal Server Error", 
 
    "exception": "org.springframework.dao.DataIntegrityViolationException", 
 
    "message": "PreparedStatementCallback; SQL [INSERT INTO employeedetails(Name, Company, Location,Age) VALUES (?,?,?,?)]; Column 'Name' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Name' cannot be null", 
 
    "path": "/createEmployee" 
 
}

在Angularjs

$http({ 
 
\t \t  method: 'POST', 
 
\t \t  url: "/createEmployee", 
 
\t \t  headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
 
\t \t  data: $scope.employee, 
 
\t \t }).success(function() {});

HTTP调用

\t 
 
\t @RequestMapping(value="/createEmployee", method=RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
 
\t public void createEmployee(employeeDetails empl) 
 
\t { 
 
\t \t System.out.println(empl.getAge()); 
 
\t \t empdao.createEmployee(empl); 
 
\t }

+0

因为在你的春天API表示您接受媒体类型 '应用程序/ x-WWW的形式了urlencoded' 而不是 '表单数据'。如果你想要它作为表单数据发送,那么你需要改变你的spring API。 – kaushlendras

+0

你可以给我代码改变吗 –

+0

在哪种类型中,你想发送数据? 'application/x-www-form-urlencoded'或'form-data'? – kaushlendras

回答

0
@RequestMapping(value="/createEmployee", method=RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
public void createEmployee(@RequestBody @Valid employeeDetails empl) 
{ 
    System.out.println(empl.getAge()); 
    empdao.createEmployee(empl); 
} 
+0

请编辑您的答案,包括一些解释。仅有代码的答案对未来SO读者的教育很少。您的答案可能会因为低质量而进入审核队列。 – Jens