Reputation: 457
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;
}
}
Upvotes: 1
Views: 1580
Reputation: 124441
public Player getPlayer(Player requestEntity) { ... }
This is your request handling method (omitting the @RequestMapping
annotation here). With this method you could do parameter binding to objects. So if you would pass the URL /player/?id=2&name=foo
then the id
field would get the value 2
and the name
field value foo
.
However you don't want to do binding you want to do message conversion. To enable this your method argument has to be annotated with @RequestBody
this will tell Spring MVC not the use parameter binding but try and detect a suitable HttpMessageConverter
to convert the HTTP message body to a Player
instance.
public Player getPlayer(@RequestBody Player requestEntity) { ... }
The signature above should fix your issue. See also the reference guide which explains this quite nicely.
Note: @RestController
is a combination of @Controller
and @ResponseBody
it does not infer the @RequestBody
. See the reference guide.
Upvotes: 2