我想使用RestEasy客户端框架测试我的REST服务。在我的应用程序中,我使用的是基本身份验证。根据RestEasy文档,我使用org.apache.http.impl.client.DefaultHttpClient来设置身份验证的凭据。
对于HTTP-GET请求,这可以很好地工作,我被授权了,并且我得到了我想要的结果响应。
但是,如果我想在请求的HTTP-Body中创建一个带有Java对象(XML格式)的HTTP-Post/HTTP-Put,该怎么办?当我使用org.apache.http.impl.client.DefaultHttpClient时,有没有一种方法可以自动将Java对象编组到HTTP中
下面是我的身份验证代码,谁能告诉我如何在不编写XML-String或使用InputStream的情况下创建一个HTTP-Post/HTTP-Put?
@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
}是否有可能使用服务器端模拟框架,然后编组并将我的对象发送到那里?
发布于 2012-10-24 00:45:33
好了,开始工作了,这就是我的新代码:
@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);
}https://stackoverflow.com/questions/12972640
复制相似问题