【发布时间】:2021-05-16 11:31:25
【问题描述】:
我使用 Spring Boot 创建了以下 REST API。
@RestController
public class PersonController {
@Autowired
private PersonRepository PersonRepository;
@PostMapping(value="/Person", produces = "application/json")
public ResponseEntity<Person> addPerson(@RequestBody Person Person){
Person newPerson = PersonRepository.save(Person);
return new ResponseEntity<Person>(newPerson, HttpStatus.OK) ;
}
}
在 Postman 中我选择:
POST > http://localhost:8080/person > json
然后在正文字段中,我放置以下 json 并点击发送:
{"id":"", "name":"john", "age":"40", "email":"test@test.com"}
这是我得到的回报:
{
"name": "john",
"age": 40,
"email": "test@test.com",
"_links": {
"self": {
"href": "http://localhost:8080/person/10"
},
"person": {
"href": "http://localhost:8080/person/10"
}
}
}
但是,我希望 json 以这种其他格式返回:
{
"id": 10,
"name": "john",
"age": 40,
"email": "test@test.com"
}
怎么做?
【问题讨论】:
标签: java json spring-boot api rest