RestEasy Client Authentication and HTTP Marshalling Connection

I want to test my REST service using the RestEasy Client Framework. In my application, I use Basic Authentication. According to the RestEasy documentation, I use org.apache.http.impl.client.DefaultHttpClient to set credentials for authentication.

For the HTTP-GET Request, this works fine, I logged in and get the Response result that I wanted.

But what if I want to make an HTTP message / HTTP-Put with a Java object (in XML) in an HTTP request tag? Is there a way to automatically marshall Java Object in HTTP-Body when I use org.apache.http.impl.client.DefaultHttpClient ?

Here is my authentication code, can someone tell me how to do HTTP Post / HTTP Put without writing an XML String or using an InputStream?

 @Test public void testClient() throws Exception { DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(username, password)); ApacheHttpClient4Executor executer = new ApacheHttpClient4Executor( client); ClientRequest request = new ClientRequest(requestUrl, executer); request.accept("*/*").pathParameter("param", requestParam); // This works fine ClientResponse<MyClass> response = request .get(MyClass.class); assertTrue(response.getStatus() == 200); // What if i want to make the following instead: MyClass myClass = new MyClass(); myClass.setName("AJKL"); // TODO Marshall this in the HTTP Body => call method } 

Is it possible to use the Mock platform on the server side and then marshall and send your object there?

+4
source share
1 answer

Ok, it works, here is my new code:

 @Test public void testClient() throws Exception { DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(username, password)); ApacheHttpClient4Executor executer = new ApacheHttpClient4Executor( client); RegisterBuiltin.register(ResteasyProviderFactory.getInstance()); Employee employee= new Employee(); employee.setName("AJKL"); EmployeeResource employeeResource= ProxyFactory.create( EmployeeResource.class, restServletUrl, executer); Response response = employeeResource.createEmployee(employee); } 

EmployeeResource:

 @Path("/employee") public interface EmployeeResource { @PUT @Consumes({"application/json", "application/xml"}) void createEmployee(Employee employee); } 
+2
source

Source: https://habr.com/ru/post/1440755/


All Articles