代码之家  ›  专栏  ›  技术社区  ›  jla

Restlet将422与Post(“xml”)一起返回到domsrepresentation

  •  0
  • jla  · 技术社区  · 6 年前

    我在restlete应用程序中使用Restlet ServerResource来处理XML post。如果我用@Post注释并取一个表示参数,text/xml和application/xml都可以接受。即使将注释更改为@Post(“xml”),也可以接受这两种类型。如果我将参数更改为DomRepresentation,那么application/xml将被拒绝,并显示422状态代码和消息:

    我使用Postman进行测试,只需在原始XML(application/XML)和XML(text/XML)之间更改主体类型。这个 documentation appears to indicate 那个org.restlet.ext.xml.dom表示法处理应用程序“所有”XML、“应用程序”XML和文本“XML”。

    这两种情况都可以:

    @Post
    public Representation doPost(Representation entity) {
        return entity;
    }
    

    这就是:

    @Post("xml")
    public Representation doPostXml(Representation entity) {
        return entity;
    }
    

    @Post("xml")
    public Representation doPostXml(DomRepresentation entity) {
        return entity;
    }
    

    当作为text、text/plain、text/xml和text/html类型发布时,domsrepresentation将接受xml文档,而不是application/xml。

    我试过XmlRepresentation。

    @Post
    public Representation doPost(XmlRepresentation entity) {
        return entity;
    }
    

    服务器拒绝为请求提供服务,因为请求实体的格式不受所请求方法所请求资源的支持。

    重新出租EE 2.3.12maven.restlet.com网站使用Java1.8托管在Tomcat8.5容器中。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Abhishek Oza    6 年前

    环境:JDK1.8
    编译器级别:Java 1.7
    Jar文件:(1)org.restlet.ext.xml.jar文件(二)org.restlet.jar

    代码:

    package com.arzoo.resources;
    
    import org.restlet.Server;
    import org.restlet.data.Protocol;
    import org.restlet.ext.xml.DomRepresentation;
    import org.restlet.representation.Representation;
    import org.restlet.resource.Post;
    import org.restlet.resource.ServerResource;
    
    public class TestXml extends ServerResource
    {   public static void main(String[] args) throws Exception
        {   // Create the HTTP server and listen on port 8182
            new Server(Protocol.HTTP, 8182, TestXml.class).start();
        }
    
        @Post("xml")
        public Representation doPostXml(DomRepresentation entity)
        {   return entity;
        }
    }
    

    来自命令行的HTTP请求:

    ~$ curl -i -H "Content-Type: application/xml" "http://localhost:8182" -d "<MyRoot/>"
    

    HTTP/1.1 200 OK
    Server: Restlet-Framework/2.3.12
    Date: Fri, 09 Nov 2018 12:06:28 GMT
    Transfer-encoding: chunked
    Content-type: application/xml
    Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
    
    <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><MyRoot/>