user630209
user630209

Reputation: 1207

XMl Request returns bad request response where as json request works

REST service which can consume both XML and JSON. Where JSON input works fine. XML request return Bad Request error. What can be done to accept XML request.

It is an old project using spring 3 & jdk 6

@RequestMapping(value = URIConstants.EST, method = RequestMethod.POST ,  consumes={"application/json", "application/xml"}, produces={"application/xml","application/json"})      
    public @ResponseBody ResponseEntity<?> getMasEstablishments(@RequestBody EstDto msaEstRequestDto) throws MasterException {

    }

This is the dto that is used for mapping input.
//EstDto

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EstDto implements Serializable {
    private long regionCode;
    private String medicalReportYN;

    }

These are the few entries in pom.xml relevant to mapping

Pom.xml
-----------

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.7.1</version>
</dependency> 

On passing input as json the service returns the result as expected

On XML request

<?xml version="1.0" encoding="UTF-8"?>
<root>

   <medicalReportYN>N</medicalReportYN>
   <regionCode>50001</regionCode>
</root>

Error XML Response

<HTML>
    <HEAD>
        <TITLE>Error 400--Bad Request</TITLE>
    </HEAD>
    ---------------------------

     <P>
                                <FONT FACE="Courier New">The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.</FONT>
                            </P>
                        </FONT>
                    </TD>
                </TR>
            </TABLE>
        </BODY>
    </HTML>

Let me know if you need any more details. It works fine with the existing configuration for JSON data. To make it work for XML input what change need to be added ?

Upvotes: 0

Views: 323

Answers (1)

Doug Breaux
Doug Breaux

Reputation: 5105

I think you need your XML to use a root element of <estDto>, not an element named, "root".

(Either that or change your annotation to @XmlRootElement(name="root"), but the other naming is more normal and clear.)

Upvotes: 1

Related Questions