I'm using REST using Spring & Java 1.7
I've below model class:
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
And my controller is mapped to GET /person/info API. So, the controller fetches name & shows it as JSON response.
Here is the controller:
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);
}
I'm getting the JSON response as shown below:
{"name":"Jack"}
The problem is here is that this name string must be personName as shown below:
{"PersonName":"Jack"}
I believe it is taking the variable name from model & sending it as it is. Can any one tell me if it is possible to have different attribute name as "PersonName" with some annotation change in REST service??
I'll appreciate if someone can shed some light here!
Thanks!