400 bad request for rest application/xml

12,242

This

<?xml version="1.0" encoding="UTF-8">
<noSave>Save</noSave>
<dateCre>14/12/2014</dateCre>

is not valid XML. An XML document should have only one root element. An in the case of your POJO mapping, the root element should be <Creation>. So try it with

<?xml version="1.0" encoding="UTF-8"?>
<Creation>
    <noSave>sdfshd</noSave>
    <dateCre>3840389</dateCre>
</Creation>

You should also add @XmlRootElement to your class

@XmlRootElement(name = "Creation")
public class Creation {

Same for MyResponse

@XmlRootElement(name = "MyResponse")
public class MyResponse {

With XML you will get a return

<MyResponse>
    ...
</MyResponse>

No way around that. That's how XML works.


UPDATE

You're also missing a ? at the end of your xml header.

<?xml version="1.0" encoding="UTF-8"?>   // See the `?`. You are missing that

Though the header is not even required. Simply sending

<Creation>
    <noSave>sdfshd</noSave>
    <dateCre>3840389</dateCre>
</Creation>

I've tested this and it works fine

Share:
12,242
RK3
Author by

RK3

Updated on June 04, 2022

Comments

  • RK3
    RK3 almost 2 years

    Am developing simple rest webservices with jersey frame work. Written @POST method which consumes and produces both { MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML }. Using chrome Advance rest client to invoke the web services. Webservices is working as expected for below application/json request

    {"noSave": "Save", "dateCre":"14/12/2014"} 
    

    but getting 400 bad request for the below appliation/xml request

    <?xml version="1.0" encoding="UTF-8">
    <noSave>Save</noSave>
    <dateCre>14/12/2014</dateCre>
    

    there are no compilation errors in the code. Any help in resolving the below issue is appreciated. below is the code i have written

    request object:

    import java.math.BigDecimal;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Creation", propOrder = {
    
    })
    public class Creation {
    
        @XmlElement(required = true)
        protected String noSave;
        @XmlElement(required = true)
        protected String dateCre;
    
        public String getNoSave() {
            return noSave;
        }
    
        public void setNoSave(String value) {
            this.noSave = value;
        }
    
        public String getDateCre() {
            return dateCre;
        }
    
        public void setDateCre(String value) {
            this.dateCre = value;
        }
    }
    

    response object:

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "MyResponse", propOrder = {
    
    })
    public class MyResponse {
    
        @XmlElement(required = true)
        protected String resString;
        public String getResString() {
            return resString;
        }
        public void setResString(String value) {
            this.resString = value;
        }
    }
    

    rest webservice:

    import javax.validation.Valid;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    @Path("/create")
    public class CreateRequest {
    
        @POST
        @Produces({  MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
        @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
        public Response createReq( @Valid Creation request)
        {
            System.out.println(" Request "+request.getNoSave());
            MyResponse result = new MyResponse();
            result.setResString(" Created with Number 123");
            return Response.ok(result).build();
        }
    }
    

    Below is the error am getting in the chrome rest client

    Status 400 Bad Request. Loading time: 4250
    Request headers User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like      Gecko) Chrome/39.0.2171.95 Safari/537.36
    Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
    Content-Type: application/xml 
    Accept: */*
    Accept-Encoding: gzip, deflate
    Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
    Response headers Server: Apache-Coyote/1.1 
    Content-Type: text/html;charset=utf-8 
    Content-Language: en 
    Content-Length: 990 
    Date: Tue, 13 Jan 2015 13:53:11 GMT 
    Connection: close
    

    i also tried below piece of code

    @XmlRootElement(name="Creation")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Creation", propOrder = {
    
    })
    public class Creation {
    

    and below xml request. But getting the same error

    <?xml version="1.0" encoding="UTF-8">
    <Creation>
        <noSave>NoSave</noSave>
        <dateCre>14/12/2014</dateCre>
    </Creation>
    

    below error in the logs is displayed

    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Creation"). Expected elements are <{http://check.com/rrs}Creation>
    
  • RK3
    RK3 over 9 years
    Gave the below xml request after adding @XmlRootElement annotation to the Creation object. But got the same issue 400 Bad Request <?xml version="1.0" encoding="UTF-8"> <Creation> <noSave>NoSave</noSave> <dateCre>14/12/2014</dateCre> </Creation>
  • RK3
    RK3 over 9 years
    i tried with "?" <?xml version="1.0" encoding="UTF-8"?> and also without the header. stil getting the error 400 bad request. I have updated my post with the changed pojo.
  • Paul Samsotha
    Paul Samsotha over 9 years
    If your getting a 400, most likely there is an exception being logged in the server. We need to see that. 400 can mean anything. I've tested with the exact code above and it works fine.
  • RK3
    RK3 over 9 years
    Am getting this error at server log javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Creation"). Expected elements are <{check.com/rrs}Creation>
  • Paul Samsotha
    Paul Samsotha over 9 years
    Did you happen to create your JAXB classes with xjc compiler? If not, do you have a package-info.java file with the @XmlSchema defined? If so, the xml is expecting the namespace. The xml should look like <Creation xmlns="http://check.com/rrs">
  • RK3
    RK3 over 9 years
    Yes i had generated objects with xjc compiler. Including the namespace in the xml solved the issue.