我已关注this tutorial使用 Spring Boot 构建 REST API。我已经让它运行并响应我的调用,但它显示的信息有问题。当我要求输入时,我希望看到类似的内容
{
"id": 1,
"name": "petName",
"photo": "meh",
"status": "Meh"
},
但是我得到了
{
"id": 1,
"photo": "meh",
"status": "Meh"
},
我完全不知道为什么。
休息 Controller
@RestController
@RequestMapping("/pet")
class PetRestController {
private final PetRepo petRepo;
@RequestMapping(value="/{petId}", method = RequestMethod.GET)
Pet getPet(@PathVariable Long petId) {
return this.petRepo.findOne(petId);
}
@RequestMapping(value="/all", method = RequestMethod.GET)
List<Pet> getPets() {
return this.petRepo.findAll();
}
@RequestMapping(value="/delete/{petId}", method = RequestMethod.DELETE)
void deletePet(@PathVariable Long petId) {
this.petRepo.delete(petId);
}
@RequestMapping(value="/add", method = RequestMethod.POST)
void addPet(@RequestParam String name, @RequestParam String photo, @RequestParam String status) {
Pet pet = new Pet(name, photo, status);
this.petRepo.save(pet);
}
@Autowired
PetRestController(PetRepo petRepo){
this.petRepo = petRepo;
}
}
Pet.java
@Entity
public class Pet {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getPhoto() {
return photo;
}
public String getStatus() {
return status;
}
@JsonIgnore
public String name;
public String photo;
public String status;
public Pet(String name, String photo, String status) {
this.name = name;
this.photo = photo;
this.status = status;
}
Pet() {
}
}
大家有什么想法吗?
最佳答案
您有一个注释告诉 Spring 不要将 name
序列化为 JSON。删除注释,
// @JsonIgnore // <-- remove this.
public String name;
关于java - Spring Boot REST API - 显示信息的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37364204/