RestEasy客户端身份验证和HTTP放置编组 [英] RestEasy Client Authentication and HTTP Put with Marshalling
问题描述
我想使用RestEasy客户端框架测试我的REST服务。
在我的应用程序中,我使用的是基本身份验证。根据RestEasy文档,我使用 org.apache.http.impl.client.DefaultHttpClient
来设置身份验证的凭据。
I want to test my REST-Service using the RestEasy Client Framework.
In my application I am using Basic Authentication. According to the RestEasy documentation I am using the org.apache.http.impl.client.DefaultHttpClient
to set the Credentials for Authentication.
对于HTTP-GET请求,这个工作正常,我被授权,我得到了我想要的结果响应。
For an HTTP-GET Request this works fine, I am authorized and I get the result Response which I wanted.
但是,如果我想在请求的HTTP主体中使用Java对象(XML格式)创建HTTP-Post / HTTP-Put,该怎么办?当我使用 org.apache.http.impl.client.DefaultHttpClient
?
But what if I want to make a HTTP-Post/HTTP-Put with an Java Object (in XML) in the HTTP-Body of the Request? Is there a way to automatically marshall the Java Object into the HTTP-Body when I am using the org.apache.http.impl.client.DefaultHttpClient
?
这是我的身份验证代码,有人可以告诉我如何在不编写XML字符串或使用InputStream的情况下创建HTTP-Post / HTTP-Put吗?
Here's my code for authentication, can someone tell me how to make an 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 there maybe a possibillity to use the Server-side Mock Framework and then marshall and send my object there?
推荐答案
好的,让它工作,多数民众赞成我的新代码:
@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);
}
这篇关于RestEasy客户端身份验证和HTTP放置编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!