Java 學習記錄137 — 員工管理系統專案練習-Creating Add Employee REST API

前面已經完成跟驗證過第一隻 API 了

接著繼續寫,新增員工的 API

EmployeeController.java

// creat employee
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}

寫完了,還滿簡單的吧

測試方法:

打開 Postman,記得改成 http://localhost:8080/api/v1/employees

在 Headers 裡新增一組

KYE:Content-Type

VALUE:application/json

接者在 BODY 裡,填上要新增員工的內容,例如:

{
"firstName": "Chris",
"lastName": "Evans",
"emailId": "Chris_Evans@gmail.com"
}

看到顯示 Status:200 OK

去資料庫看,應該會發現資料已經被新增了

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store