4

我正在使用 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”?

如果有人可以在这里阐明一下,我将不胜感激!

谢谢!

4

1 回答 1

0

如果您使用的是 Jackson 库,我想您可以这样指定:

@JsonProperty("PersonName")
public String getName() {
    return name;
}
于 2013-02-15T00:28:42.243 回答