I am trying to make a restful Webservice that takes XML request and produces xml response and I am using spring 4.3.5 for it...
The issue is that when I am posting a post request using postman and add a debug on in my service method than I found that xml request attributes is not mapping/deserialize with player POJO.
Any help would be appreciated..Thanks
@RestController
public class SyncRestfulService {
LoggerManager loggerManager = new LoggerManager();
@RequestMapping(value = RestURIConstants.SAMPLE_POST_PLAYER, method = RequestMethod.POST, headers = {
"Content-type=application/xml" })
public Player getPlayer(Player requestEntity) {
loggerManager.info(LoggerConstantEnum.SyncRestfulService ,
"| <AbilitySyncRestfulService> Method: getPlayer " + requestEntity);
loggerManager.info(LoggerConstantEnum.SyncRestfulService , "Id : ", requestEntity.getId());
return requestEntity;
}
}
Request xml data
//Request
<player>
<id>1</id>
<matches>251</matches>
<name>Anand</name>
</player>
//response xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<player>
<id>0</id>
</player>
//jaxb pojo
@XmlRootElement(name = "player")
@XmlAccessorType(XmlAccessType.NONE)
public class Player {
private int id;
private String name;
private String matches;
public int getId() {
return id;
}
@XmlElement(name = "id")
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public String getMatches() {
return matches;
}
@XmlElement(name = "matches")
public void setMatches(String matches) {
this.matches = matches;
}
}
@RequestBodyannotation on thePlayerargument in your controllers method. Spring isn ow merely creating a new instance to do parameter binding, but as you aren't passing parameters in the url (or as form) nothing will be bound. You ar actually passing XML as the body and you need to instruct spring to do marshaling for that (instead of binding).@RestControlleris a combination of@Controllerand@ResponseBodyNOT@RequestBody. See docs.spring.io/spring/docs/current/spring-framework-reference/… (which explains just that).