【发布时间】:2013-02-15 00:25:57
【问题描述】:
我正在使用 Spring 和 Java 1.7 的 REST
我有以下模型类:
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”??
如果有人能在这里阐明一下,我将不胜感激!
谢谢!
【问题讨论】: