简体   繁体   English

将值传递给RESTful Web服务发布方法

[英]Passing values to RESTful web service post method

I am successfully created restful web service and deploy it in Apache Tomcat 7.0. 我已经成功创建了Restful Web服务并将其部署在Apache Tomcat 7.0中。 After successful deployment I start my server. 成功部署后,我启动服务器。 By using the below command i invoke the web service. 通过使用以下命令,我调用了Web服务。

WebResource resource = client.resource("http://localhost:8080/rest/samp/create"); 

My web method is 我的网络方法是

@POST
@Path("/create")
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.TEXT_XML)
public final String sample(final String xmlMessage) {

    return "<xml version=1.0><welcome>"+xmlmessage+"</welcome>";    
}

Here I am passing XML content as argument and get the XML content as response. 在这里,我将XML内容作为参数传递,并将XML内容作为响应。

Now what I need is how to pass the XML content to the web method. 现在,我需要的是如何将XML内容传递给Web方法。

You can try Curl : 您可以尝试Curl:

http://curl.haxx.se/

simple command line to send data with post : 使用post发送数据的简单命令行:

curl -d "here puts data" -X POST http://mysite/create

I'm guessing that the library in use here is Jersey. 我猜这里使用的库是泽西岛。

You have to use a builder to set the appropriate HTTP headers, method and entity body. 您必须使用构建器来设置适当的HTTP标头,方法和实体主体。

WebResource resource = client.resource("http://localhost:8080/rest/samp/create");
String request = "<your_xml>...</your_xml>";
String response = resource.accept(
     MediaType.TEXT_XML).
     header("X-FOO", "BAR"). //this line is not necessary, just an example
     type(MediaType.TEXT_XML).
     post(String.class, request);

But I recommend using JAXB instead. 但是我建议改用JAXB。 Creating XML as plain strings is just crude and unnecessarily annoying. 将XML创建为纯字符串只是粗略且不必要的烦人。 It doesn't show in such a simple example (grabbing a whole XML and wrapping it with another tag) but it will soon enough. 它没有在这样一个简单的示例中显示(获取整个XML并将其与另一个标签包装在一起),但是很快就可以了。

This tutorial should get you started. 本教程将帮助您入门。

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

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