1

enter image description hereI 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;
    }
}
8
  • add produces = { MediaType.APPLICATION_XML_VALUE } in your controller Commented May 8, 2017 at 10:26
  • and do u have Configure bean to convert XML to POJO and vice versa in application-context.xml file Commented May 8, 2017 at 10:27
  • Correct if I am wrong, I think in spring 4 we are not required to add a converter. and its producing XML correctly I have tested that but In case issue is in mapping post body xml string to "Playter playter" Commented May 8, 2017 at 10:32
  • 1
    It is actually working as designed... The main problem here is that you forgot the @RequestBody annotation on the Player argument 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). Commented May 8, 2017 at 10:35
  • 1
    @AnandKushwaha @RestController is a combination of @Controller and @ResponseBody NOT @RequestBody. See docs.spring.io/spring/docs/current/spring-framework-reference/… (which explains just that). Commented May 8, 2017 at 10:57

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.