我正在研究Spring Guides教程:使用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"}
这是控制器代码
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);
}
}
我听了卡罗尔和杰斯珀的建议。感谢双方的快速响应。我的新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
答案 0 :(得分:1)
同时指定Content-Type: application/json
和Accept: application/json
请求标头,因为您的端点正在使用和生成数据。
curl -H 'Content-Type: application/json' -H 'Accept: application/json' ...
答案 1 :(得分:1)
在本教程中,这也在我这边发生: https://spring.io/guides/tutorials/rest/
奇怪的是,在Linux OS上却没有发生。我仔细检查了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\"}"
总结: 将所有的'更改为“,然后在正文中添加\
希望对您有帮助!
芬果
答案 2 :(得分:0)
除了上述可行的答案之外,我还想让人们意识到与Springboot 2相关的该教程代码中的一个小错误。
遵循这里描述的卷曲建议后,我得到了405。
我发现需要对注释进行细微调整:
@PostMapping(value="/employees",produces = "application/json")
Employee newEmployee(@RequestBody Employee newEmployee) {
return repository.save(newEmployee);
}
请注意,需要使用产生的标志来使其正确响应,以免出现405。与curl命令结合使用时:
curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "{\"name\": \"Samwise Gamgee\", \"role\": \"gardener\"}" http://localhost:8080/employees