2

Created a spring boot rest service which accepts XML as a request and converts it into object and then inserts it into database. I used Jackson dataformat dependency for directly converting the xml request to object but getting an error .

The controller class is as follows

@RequestMapping(value = "/getRequestData",method=RequestMethod.POST,
        produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},consumes={MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<?> putDataIntoDatabase(@RequestBody FirstRequestorBean bean) {

        logger.info(bean.getId());
        return new ResponseEntity<String>(HttpStatus.OK);
    }

and bean class

public class FirstRequestorBean {


    private String name;
    private String id;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }



}

the xml passed here is

<?xml version="1.0" encoding="UTF-8"?>
<FirstRequestorBean>
    <name>Akhil</name>
    <id>1</id>
</FirstRequestorBean>  

getting a Unsupported Media Type exception.

can someone point out how to resolve the issue

4
  • Try removing your XML file header <?xml version="1.0" encoding="UTF-8"?> and try adding MediaType.TEXT_XML. Commented Jul 27, 2017 at 11:15
  • produces = {MediaType.TEXT_XML_VALUE},consumes={MediaType.TEXT_XML_VALUE}) tried adding this @CrazySabbath .still the same error Commented Jul 27, 2017 at 11:33
  • Either you misspelled your comment, or you didn't understand me. Have you tried adding TEXT_XML (notice, not TEXT_XML_VALUE) and removing xml header? Commented Jul 27, 2017 at 11:35
  • @CrazySabbath yes. I tried removing xml header and using the Mediatype.TEXT_XML .still fetched me the same error Commented Jul 27, 2017 at 11:42

2 Answers 2

2

Your Post request will have a "content-type" which needs to match up to the "consumes" parameter for your request mapping.

For XML there are two types "application/xml" and "text/xml" it is good practice to accept both, (MediaType.APPLICATION_XML_VALUE & MediaType.TEXT_XML_VALUE).

Additionally you have: produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} this is your return type, you only need to define this once you are actually returning something, seeing as are only setting a status code then this should be removed.

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

4 Comments

I have tried both the specific steps but still getting an error
What is the content type for your request?
Hi MartinByers, I was able to resolve the issue ,supposedly was using a tool Jmeter for which I had not set the appropriate Accept Headers , after which it worked.More of a tool settings issue then it was of code.Thank you for the support
@MartinByers - Could you please guide me here: stackoverflow.com/questions/59485326/…?
-1
  1. Add MediaType.APPLICATION_XML_VALUE to accept XML form of data on your request mapping. Ex:- (Here application accepts both JSON and XML format as request) @PostMapping(value = "/users/add", consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
  2. Add @XmlRootElement annotation to your bean class
  3. Add appropriate Content-Type (application/json or application/xml) in the header of your request

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.