简体   繁体   中英

http POST request to jax-rs web service

I have a jax-rs web service in grails framework that respond to PUT requests and consumes/produces xml or json but when i try to PUT a xml to web service via chrome's advanced rest plugin it gives error 415 Unsupported Media Type .
I want to know how can i PUT a xml into web service?
Note: I am using application/xml as content type My code for web service is:

     @Path('/api/interface')
        @Consumes(['application/xml','application/json'])
        @Produces(['application/xml','application/json'])
        class InterfaceResource {

        @POST
            @Path("xmldata")
            String  getInterfaceRepresentationXML(def xml) {
                //play with xml and render xml result
            }
        }

So the link would be xxxx/api/interface/xmldata and that does not accept XML file . Please help, thanks in advance.

did you set the content-type to application/xml instead of the default application/x-www-form-urlencoded ?

or, do you have another endpoint in your application with the following URL Template:

x.x.x.x/api/interface/{param} 

which also consumes PUT requests but does not accept application/xml content type ? (I'm not sure which resource method would be selected by the JAX-RS implementation, though)

On the server side

@Consumes and @Produces directives should be tied to the method. Also, verify your required http method is @POST (PUT method is rarely used in such cases, but you are free to use PUT if you really want it this way).

@POST
@Path("xmldata")
@Consumes('application/xml')
@Produces('application/xml')
String getInterfaceRepresentationXML(def xml) {
    //play with xml and render xml result
}

On the client side

Be sure your client use the correct http method (POST or PUT, as declared on sever side). Providing xml data in the http post content is not sufficient, you must also inform the server on what kind of data you are giving to it. You must provide the content-type in the http headers.

In chrome's advanced rest client, specify in the headers:

Content-Type: application/xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM