Spring Boot REST API - 显示信息的问题

时间:2016-05-21 14:32:02

标签: java spring rest spring-mvc

我已跟随this tutorial使用Spring Boot构建REST API。我已经让它运行并响应我的呼叫,但它显示的信息存在问题。当我要求参赛作品时,我期待的是

Your Name: <input type="name" name="name" id="name" ><br><BR>
Your Contact No. <input type="contact" name="contact" id="contact" ><br><BR>
Todays date:<mark>{{$mytime}}</mark><br><br>
tomorrows date:{{$tomorrow}}
Book for: <select>
  <option name="select" value="{{$mytime}}">Today</option>
  <option name="select" value="{{$tomorrow}}">Tomorrow</option>
  <option name="select" value="{{$nextday}}">{{$nextday}}</option>
  <option name="select" value="{{$nextday}}">{{$nextday}}</option>
</select>

但我得到了

{
"id": 1,
"name": "petName",
"photo": "meh",
"status": "Meh"
},

我完全不确定原因。

RestController

{
"id": 1,
"photo": "meh",
"status": "Meh"
},

Pet.java

@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;
}
}

有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你有一个注释告诉Spring name序列化为JSON。删除注释,

// @JsonIgnore // <-- remove this.
public String name;