拥有JSON响应中属性的特定名称



我使用Spring使用REST;Java 1.7

我有以下型号类别:

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

我的控制器被映射到GET/person/info API。因此,控制器获取名称&将其显示为JSON响应。

这是控制器:

 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping(value = "persons/info", method = RequestMethod.POST, consumes="application/json", produces="application/json")
public ResponseEntity<String> getPersonInfo(@RequestBody String body) throws Exception {    
    String personInfo = null;
    MyServiceJson myServiceJson = MyServiceJsonFactory.getMyServiceObject(body);
    personInfo = myService.getPersonInfo(myServiceJson);
    HttpHeaders responseHeader = new HttpHeaders();
    return Util.getResponse(personInfo, responseHeader, HttpStatus.OK);
}

我得到了如下所示的JSON响应:

{"name":"Jack"}

这里的问题是这个名称字符串必须是personName,如下所示:

{"PersonName":"Jack"}

我相信它是从模型&按原样发送。有人能告诉我,在REST服务中进行一些注释更改后,是否可以将不同的属性名称设置为"PersonName"吗??

如果有人能在这里发光,我将不胜感激!

谢谢!

如果你正在使用Jackson库,我想你可以用这种方式进行spcify:

@JsonProperty("PersonName")
public String getName() {
    return name;
}

最新更新