我正在学习 Spring 指南教程:使用 Spring 构建 Rest 服务。
我已经按照文本操作并在教程中输入了代码。
我到了启动服务(在本地机器上)并使用 CURL 命令进行测试的部分。
GET 工作正常:
Curl -v localhost:8080/employees
返回预期列表
[{"id":1,"name":"Bilbo Baggins","role":"burglar"},{"id":2,"name":"Frodo Baggins","role":"thief"}]
但是,当我执行:
curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Samwise Gamgee", "role": "gardener"}'
我得到:
{"timestamp":"2018-11-08T20:55:49.844+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/employees"}
这是 Controller 代码
package com.mainsworth.payroll;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
class EmployeeController {
private final EmployeeRepository repository;
EmployeeController(EmployeeRepository repository) {
this.repository = repository;
}
@GetMapping("/employees")
List<Employee>all() {
return repository.findAll();
}
@PostMapping("/employees")
Employee newEmployee(@RequestBody Employee newEmployee) {
return repository.save(newEmployee);
}
//Single item
@GetMapping("/employees/{id}")
Employee one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(()-> new EmployeeNotFoundException(id));
}
@PutMapping("/employees/{id}")
Employee replaceEmployee(@RequestBody Employee newEmployee,
@PathVariable Long id ) {
return repository.findById(id)
.map(employee -> {
employee.setName(newEmployee.getName());
employee.setRole(newEmployee.getRole());
return repository.save(employee);
})
.orElseGet(() -> {
newEmployee.setId(id);
return repository.save(newEmployee);
});
}
@DeleteMapping("/employees/{id}")
void deleteEmployee(@PathVariable Long id) {
repository.deleteById(id);
}
}
我听从了 Karol 和 Jesper 的建议。感谢两位的快速响应。我的新 curl 是:
curl -X POST localhost:8080/employees -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"name": "Samwise Gamgee","role": "gardener"}'
我的新回复是:
{"timestamp":"2018-11-08T22:49:01.900+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8'
not supported","path":"/employees"}application
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: Samwise Gamgee,role
curl: (3) [globbing] unmatched close brace/bracket in column 9
最佳答案
在本教程中,它也在我身边发生:
https://spring.io/guides/tutorials/rest/
奇怪的是在 Linux 操作系统上它没有发生。我仔细检查了 Linux,一切都很好。
解决方案(示例):
反而:
curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Samwise Gamgee", "role": "gardener"}'
用:
curl -X POST localhost:8080/employees -H "Content-type:application/json" -d "{\"name\": \"Samwise Gamgee\", \"role\": \"gardener\"}"
总结:
将所有 ' 更改为 "并在正文中在每个 "之前添加\
我希望它有帮助!
芬菲尔
关于java - Curl 在 JetBrains IDE (Windows) 中无法用于 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53216437/