2010-05-20 156 views
8

我正在使用RESTEasy客户端框架来调用RESTful Web服务。该调用通过POST进行,并将一些XML数据发送到服务器。我该如何做到这一点?如何使用RESTEasy客户端框架在POST中发送数据

注解的神奇咒语用来实现这一点是什么?

+0

@David Escandell,请你在这里发表整个例子。我能够使用XML发布数据,但无法正确序列化对象。我希望你的榜样能够帮助我很多。 – 2011-10-16 16:00:49

+2

我喜欢“注解的神奇咒语”这个问题的一部分......它是这个新工程现象的重要组成部分之一! – 2013-03-29 18:21:43

回答

0

我从这个例子中借用了:Build restful service with RESTEasy下面的代码片段,它看起来完全是你想要的,不是吗?

URL url = new URL("http://localhost:8081/user"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/xml"); 
connection.setDoOutput(true); 
connection.setInstanceFollowRedirects(false); 

StringBuffer sbuffer = new StringBuffer(); 
sbuffer.append("<user id=\"0\">"); 
sbuffer.append(" <username>Test User</username>"); 
sbuffer.append(" <email>[email protected]</email>"); 
sbuffer.append("</user>"); 

OutputStream os = connection.getOutputStream(); 
os.write(sbuffer.toString().getBytes()); 
os.flush(); 

assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode()); 
connection.disconnect(); 
12

我认为大卫指的是RESTeasy“客户端框架”。因此,你的答案(Riduidel)并不特别在寻找什么。您的解决方案使用HttpUrlConnection作为http客户端。使用resteasy客户端而不是HttpUrlConnection或DefaultHttpClient是有益的,因为resteasy客户端是JAX-RS可感知的。要使用RESTeasy客户端,需要构造org.jboss.resteasy.client.ClientRequest对象并使用其构造函数和方法构建请求。以下是我如何使用RESTeasy的客户端框架实现David的问题。

ClientRequest request = new ClientRequest("http://url/resource/{id}"); 

StringBuilder sb = new StringBuilder(); 
sb.append("<user id=\"0\">"); 
sb.append(" <username>Test User</username>"); 
sb.append(" <email>[email protected]</email>"); 
sb.append("</user>"); 


String xmltext = sb.toString(); 

request.accept("application/xml").pathParameter("id", 1).body(MediaType.APPLICATION_XML, xmltext); 

String response = request.postTarget(String.class); //get response and automatically unmarshall to a string. 

//or 

ClientResponse<String> response = request.post(); 

希望这有助于 查理

+0

这确实有帮助。 我找到的解决方案更简单。该框架允许您构建一个定义您要调用的REST方法的java接口。你可以在方法前面放置一个@Produces(“application/xml”),然后用适当的JAXB注解传递对象,并且所有东西都可以神奇地工作。 – 2010-06-01 13:39:30

+0

绝对!这种调用方式使用RESTeasy客户端代理。你可以称之为另一种“客户端框架”来实现宁静的服务。我甚至更喜欢这种方法,因为它允许您在客户机上重新使用服务器的服务接口,因此可以更轻松地进行自我记录和统一API。 – 2010-06-01 17:14:17

4

这是因为以下

@Test 
    public void testPost() throws Exception { 
     final ClientRequest clientCreateRequest = new ClientRequest("http://localhost:9090/variables"); 
     final MultivaluedMap<String, String> formParameters = clientCreateRequest.getFormParameters(); 
     final String name = "postVariable"; 
     formParameters.putSingle("name", name); 
     formParameters.putSingle("type", "String"); 
     formParameters.putSingle("units", "units"); 
     formParameters.putSingle("description", "description"); 
     formParameters.putSingle("core", "true"); 
     final ClientResponse<String> clientCreateResponse = clientCreateRequest.post(String.class); 
     assertEquals(201, clientCreateResponse.getStatus()); 
    } 
0

容易我遇到了一些麻烦搞清楚如何做到这一点,所以我想我会在这里发布。使用RESTeasy代理客户端机制实际上非常容易。

正如查尔斯Akalugwu表明,这种方法允许你创建你可以在客户端和服务器端都使用单一的Java接口,并在客户端和服务器端代码这是显而易见的,易于使用的结果。

首先,声明服务的Java接口。这将在客户端和服务器端使用,并且应包含所有JAX-RS声明:

@Path("/path/to/service") 
public interface UploadService 
{ 
    @POST 
    @Consumes("text/plan") 
    public Response uploadFile(InputStream inputStream); 
} 

接下来,编写实现此接口的服务器。它是那么容易,因为它看起来:

public class UploadServer extends Application implements UploadService 
{ 
    @Override 
    public Response uploadFile(InputStream inputStream) 
    { 
    // The inputStream contains the POST data 
    InputStream.read(...); 

    // Return the location of the new resource to the client: 
    Response.created(new URI(location)).build(); 
    } 
} 

要回答这个问题:“如何使用的RESTEasy客户端框架,以在POST发送数据”,所有你需要做的就是通过RestEasy的调用从客户端的服务接口代理和RESTeasy将为您执行POST。要创建客户端代理:

Client client = ClientBuilder.newClient(); 
WebTarget target = client.target("http://path/to/service"); 
ResteasyWebTarget rtarget = (ResteasyWebTarget)target; 
UploadService uploadService = rtarget.proxy(UploadService.class); 

到POST数据到服务:

InputStream inputStream = new FileInputStream("/tmp/myfile"); 
uploadService.uploadFile(inputStream); 

当然,如果你正在编写到现有的REST服务,那么你可以通过只写一个Java接口处理这个问题为客户。

相关问题