I am using the RESTEasy client framework to call a RESTful webservice. The call is made via a POST and sends some XML data to the server. How do I accomplish this?
What is the magical incan开发者_如何学运维tation of annotations to use to make this happen?
I think David is referring to the RESTeasy "Client Framework". Therefore, your answer (Riduidel) is not particularly what he is looking for. Your solution uses HttpUrlConnection as the http client. Using the resteasy client instead of HttpUrlConnection or DefaultHttpClient is beneficial because resteasy client is JAX-RS aware. To use the RESTeasy client, you construct org.jboss.resteasy.client.ClientRequest objects and build up requests using its constructors and methods. Below is how I'd implement David's question using the client framework from RESTeasy.
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();
Hope this helps, Charlie
It is as easy as following
@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());
}
Try this syntax:
Form form = new Form();
form
.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)
.header(HttpHeaders.AUTHORIZATION, authCreds)
.post(entity);
System.out.println("HTTP code: " + response.getStatus());
I had some trouble figuring out how to do this so I thought I would post it here. Using the RESTeasy proxy client mechanism is actually embarrassingly easy.
As Charles Akalugwu suggests, this approach allows you to create a single Java interface which you can use on both the client and server side, and results in both client- and server-side code that's obvious and easy to use.
First, declare a Java interface for the service. This will be used on both the client and server side, and should contain all of the JAX-RS declarations:
@Path("/path/to/service")
public interface UploadService
{
@POST
@Consumes("text/plan")
public Response uploadFile(InputStream inputStream);
}
Next, write a server which implements this interface. It's as easy as it looks:
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();
}
}
To answer the question "how to use RESTEasy Client Framework to send data in a POST", all you have to do is call the service interface from the client side via a RESTeasy proxy, and RESTeasy will do the POST for you. To create the client proxy:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://path/to/service");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
UploadService uploadService = rtarget.proxy(UploadService.class);
To POST data to the service:
InputStream inputStream = new FileInputStream("/tmp/myfile");
uploadService.uploadFile(inputStream);
Naturally, if you are writing to an existing REST service then you could approach the problem by writing a Java interface just for the client.
I borrowed from this example : Build restful service with RESTEasy the following code fragment, which seems to do exactly what you want, no ?
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();
精彩评论