如何使用RESTEasy客户端框架在POST中发送数据

时间:2010-05-20 21:20:34

标签: java rest resteasy

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

用于实现此目的的注释的神奇咒语是什么?

5 个答案:

答案 0 :(得分:12)

我认为David指的是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>test.user@test.com</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();

希望这有帮助, 查理

答案 1 :(得分: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());
    }

答案 2 :(得分: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>test.user@test.com</email>");
sbuffer.append("</user>");

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

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

答案 3 :(得分:0)

我弄清楚如何做到这一点所以我想我会在这里发布。使用RESTeasy代理客户端机制实际上非常简单。

正如Charles 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();
  }
}

要回答问题&#34;如何使用RESTEasy Client Framework在POST&#34;中发送数据,您只需通过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);

将数据发布到服务:

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

当然,如果您正在编写现有的REST服务,那么您可以通过为客户端编写Java接口来解决问题。

答案 4 :(得分:0)

尝试以下语法:

Form form = new Form();
form
 .param(HttpHeaders.AUTHORIZATION, "Bearer " + token)
 .param("client_id", "Test_Client")
 .param("grant_type", "password")
 .param("response_type", "code")
 .param("scope", "openid")
 .param("redirect_uri", "some_redirect_url");
Entity<Form> entity = Entity.form(form);

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/auth/realms");
Response response = target
 .request(MediaType.APPLICATION_JSON)
 .post(entity);
System.out.println("HTTP code: " + response.getStatus());