Java Jersey PUT 方法和工作客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28046752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Jersey PUT Method and working Client
提问by silenum
I am developing an Dynamic Web Application with Eclipse. I have e working MySQL-Database which is connected over a class called 'Data Access Object' (=DAO) that works with JDBC. I want to create entries into this database. The functions are ready. With ready I mean tested and OK. On the same application I implemented Java Jersey's RESTful WebService. It is working well, I can call the service and it returns my desired information. But now to my question:
我正在使用 Eclipse 开发动态 Web 应用程序。我有一个正在运行的 MySQL 数据库,它通过一个名为“数据访问对象”(= DAO)的类连接,该类与 JDBC 一起使用。我想在这个数据库中创建条目。功能已准备就绪。准备好我的意思是测试和确定。在同一个应用程序中,我实现了 Java Jersey 的 RESTful WebService。它运行良好,我可以调用该服务并返回我想要的信息。但现在我的问题:
How can I send a String containing XML? The String has to be parsed in the WebMethod to build and execute the query.
如何发送包含 XML 的字符串?必须在 WebMethod 中解析字符串以构建和执行查询。
My WebService looks as follows:
我的 WebService 如下所示:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
@Path("/input")
public class Input {
//DAO instance to have connection to the database.
//Not used yet.
//private DAO dao = new DAO();
@PUT
@Consumes(MediaType.TEXT_XML)
@Path("/result")
public void putIntoDAO(InputStream xml) {
String line = "";
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(xml));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
}
}
As you see I tried to print the incoming Stream to the console. I repeat the most important things:
如您所见,我尝试将传入的 Stream 打印到控制台。 我重复最重要的事情:
- I know how to parse XML.
- I know my DAO works properly.
- I know my WebService works as well.
- 我知道如何解析 XML。
- 我知道我的 DAO 工作正常。
- 我知道我的 WebService 也能正常工作。
What I would like to know:
我想知道的是:
- How do I send an XML-String to my WebService?
- How do I access this String in my PUT-method?
- 如何将 XML 字符串发送到我的 WebService?
- 如何在我的 PUT 方法中访问这个字符串?
Thank you for your attention and try to help me. I appreciate even every try to.
感谢您的关注并尝试帮助我。我感谢每一次尝试。
Kind regards
亲切的问候
L.
L。
采纳答案by Ross Taylor-Turner
How do I access this String in my PUT-method?
如何在我的 PUT 方法中访问这个字符串?
You can simply code the method to take an argument of type String
and Jersey will map this from the incoming XML request, so:
您可以简单地将方法编码为采用类型的参数,String
Jersey 将从传入的 XML 请求中映射它,因此:
@PUT
@Consumes(MediaType.TEXT_XML)
@Path("/result")
public void putIntoDAO(String xml) {
// ...
}
Should work, with the String containing the full request body.
应该可以使用包含完整请求正文的字符串。
How do I send an XML-String to my WebService?
如何将 XML 字符串发送到我的 WebService?
This depends on what you're using to send the request to the service, which could be anything which communicates over HTTP. I'll assume you're using Java and sticking with Jersey, so one option is you can use the Jersey Client in the following way:
这取决于您使用什么将请求发送到服务,它可以是通过 HTTP 进行通信的任何内容。我假设您使用 Java 并坚持使用 Jersey,因此一种选择是您可以通过以下方式使用 Jersey 客户端:
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/input/result");
String input = "<xml></xml>";
ClientResponse response = webResource
.type("application/xml")
.put(ClientResponse.class, input);
See the Jersey Client documentationfor more.
有关更多信息,请参阅Jersey 客户端文档。
回答by silenum
The answer Ross Turner posted is completely correct and working. Here is an option using Apache HttpComponents.
罗斯·特纳 (Ross Turner) 发布的答案完全正确且有效。这是一个使用Apache HttpComponents的选项。
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class Runner {
public static void main(String[] args) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPut putRequest = new HttpPut("http://localhost:8080/HelloFromJersey/input/result");
StringEntity input = new StringEntity("Hello, this is a message from your put client!");
input.setContentType("text/xml");
putRequest.setEntity(input);
httpClient.execute(putRequest);
httpClient.getConnectionManager().shutdown();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The server prints:
服务器打印:
Hello, this is a message from your put client!