简体   繁体   English

对jax-rs Web服务的HTTP POST请求

[英]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 . 我在grails框架中有一个jax-rs Web服务,该服务响应PUT请求并使用/产生xml或json,但是当我尝试通过chrome的高级rest插件将xml放置到Web服务时,它会给出错误415 Unsupported Media Type
I want to know how can i PUT a xml into web service? 我想知道如何将xml放入Web服务中?
Note: I am using application/xml as content type My code for web service is: 注意:我使用application / xml作为内容类型我的Web服务代码是:

     @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 . 因此,链接将为xxxx/api/interface/xmldata ,并且不接受XML文件。 Please help, thanks in advance. 请帮助,在此先感谢。

did you set the content-type to application/xml instead of the default application/x-www-form-urlencoded ? 您是否将内容类型设置为application/xml而不是默认的application/x-www-form-urlencoded

or, do you have another endpoint in your application with the following URL Template: 或者,您的应用程序中是否有另一个具有以下URL模板的终结点:

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

which also consumes PUT requests but does not accept application/xml content type ? 这也消耗PUT请求但不接受application/xml内容类型? (I'm not sure which resource method would be selected by the JAX-RS implementation, though) (不过,我不确定JAX-RS实现会选择哪种资源方法)

On the server side 在服务器端

@Consumes and @Produces directives should be tied to the method. @Consumes和@Produces指令应与该方法绑定。 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). 另外,请验证您所需的http方法是否为@POST(在这种情况下很少使用PUT方法,但是如果您确实希望以这种方式使用PUT,则可以自由使用它)。

@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). 确保您的客户端使用正确的http方法(服务器端声明的POST或PUT)。 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. 在http post内容中提供xml数据是不够的,您还必须告知服务器要提供的数据类型。 You must provide the content-type in the http headers. 您必须在http标头中提供content-type。

In chrome's advanced rest client, specify in the headers: 在chrome的高级Rest Client中,在标题中指定:

Content-Type: application/xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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