如何通过curl发送REST XML POST请求?

Naf*_*Kay 3 rest curl http jaxb http-post

基本上,我在Restlet中设置了一个项目,它使用JAXRS将资源映射到路径,并使用JAXB将XML序列化和反序列化为Java类型.我正在尝试发送POST请求以测试它是否有效,而且我遇到了一些麻烦.这是我的资源:

@Path("stream")
public class StreamResource {

    @POST
    @Consumes("text/xml")
    @Produces("text/xml")
    public Stream save(Stream value) {
        logger.debug("saving new stream...");
        return (Stream)this.streamPersistence.save(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Stream班级:

@XmlRootElement(name="stream")
@XmlType(propOrder={"id", "streamName", "title", "description", fileSystemPath"})
public class Stream {

    private Long id;

    private String streamName;

    private String fileSystemPath;

    private String title;

    private String description;

    // getters/setters omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)

以下是我的调用方式curl:

curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type:"text/xml"' http://localhost:8888/stream
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误curl:

The given resource variant is not supported.
Run Code Online (Sandbox Code Playgroud)

...这是Restlet中的错误:

15:02:25.809 [Restlet-961410881] WARN  org.restlet.Component.Server - Error while parsing entity headers java.lang.IllegalArgumentException: Illegal token: "text
    at org.restlet.data.MediaType.normalizeToken(MediaType.java:647)
    at org.restlet.data.MediaType.normalizeType(MediaType.java:686)
    at org.restlet.data.MediaType.<init>(MediaType.java:795)
    at org.restlet.data.MediaType.<init>(MediaType.java:767)
    at org.restlet.engine.http.header.ContentTypeReader.createContentType(ContentTypeReader.java:84)
    at org.restlet.engine.http.header.ContentTypeReader.readValue(ContentTypeReader.java:112)
    at org.restlet.engine.http.header.ContentType.<init>(ContentType.java:99)
    at org.restlet.engine.http.header.HeaderUtils.extractEntityHeaders(HeaderUtils.java:664)
    at org.restlet.engine.http.connector.Connection.createInboundEntity(Connection.java:313)
    at org.restlet.engine.http.connector.ServerConnection.createRequest(ServerConnection.java:136)
    at org.restlet.engine.http.connector.ServerConnection.readMessage(ServerConnection.java:229)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:673)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?这看起来很简单,对吧?

Ray*_*oal 6

删除周围的引号text/xml.

换句话说,你想要的

curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type: text/xml' http://localhost:8888/stream
Run Code Online (Sandbox Code Playgroud)