spring 3.0.5 release 对REST 客户端的应用例子
来源:互联网 发布:阿里云流量包 编辑:程序博客网 时间:2024/06/06 00:33
1.>application-xxx.xml
<bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">
<property name="authenticationPreemptive" value="true"/>
<property name="connectionManagerClass" value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/>
</bean>
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg ref="httpClientParams"/>
</bean>
<bean id="credentials" class="org.apache.commons.httpclient.UsernamePasswordCredentials">
<constructor-arg value="restuser"/>
<constructor-arg value="restbpm"/>
</bean>
<bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<constructor-arg ref="httpClient"/>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory"/>
<property name="messageConverters">
<list>
<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"></bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstreamMarshaller" />
<property name="unmarshaller" ref="xstreamMarshaller" />
</bean>
</list>
</property>
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="User">org.ow2.bonita.facade.identity.impl.UserImpl</prop>
<prop key="contactInfo">org.ow2.bonita.facade.identity.impl.ContactInfoImpl</prop>
<prop key="Membership">org.ow2.bonita.facade.identity.impl.MembershipImpl</prop>
<prop key="Role">org.ow2.bonita.facade.identity.impl.RoleImpl</prop>
<prop key="Group">org.ow2.bonita.facade.identity.impl.GroupImpl</prop>
<prop key="LightProcessDefinition">org.ow2.bonita.light.impl.LightProcessDefinitionImpl</prop>
</props>
</property>
</bean>
<bean id="userRestClient" class="com.sanminasci.bonita.admin.web.rest.UserRestClient">
<constructor-arg ref="restTemplate"/>
<constructor-arg ref="credentials"/>
</bean>
import java.util.List;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.AuthScope;
import org.ow2.bonita.facade.identity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class UserRestClient {
private RestTemplate restTemplate;
private Credentials credentials;
private String hostIp;
private int port;
public UserRestClient(RestTemplate restTemplate, Credentials credentials){
this.restTemplate = restTemplate;
this.credentials = credentials;
CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
HttpClient client = factory.getHttpClient();
client.getState().setCredentials(AuthScope.ANY, credentials);
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
public String getHostIp() {
return hostIp;
}
public void setHostIp(String hostIp) {
this.hostIp = hostIp;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public List<User> getAllBonitUser(){
MultiValueMap<String, String> map1 = new LinkedMultiValueMap<String, String>();
map1.add("options", "user:admin");
String url = "http://"+hostIp+":"+port + "/bonita-server-rest/API/identityAPI/getAllUsers";
List<User> list = restTemplate.postForObject(url, map1, List.class);
return list;
}
}
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("//scionDevTool/eclipse3.6/workspace/project- deploy-portlet/project-deploy-web/src/main/webapp/WEB-INF/spring-applicationContext-projectDeploy.xml");
UserRestClient userRestClient = applicationContext.getBean("userRestClient", UserRestClient.class);
userRestClient.setPort(8080);
userRestClient.setHostIp("172.25.165.66");
System.out.println( userRestClient.getAllBonitUser().size());
}
}
- spring 3.0.5 release 对REST 客户端的应用例子
- spring对rest的支持
- spring 官方对REST的定义
- Spring提供RestTemplate用于访问Rest服务的客户端实践
- rest 应用demo 3 客户端的另一中方式
- REST的小例子
- Spring rest对etag支持
- Spring Boot示例 - 2. 使用Spring Boot + Spring Data REST + Spring Data JPA构建符合HATEOAS规范的REST应用
- spring中对hibernate的支持例子
- 对REST的理解
- spring integration之http-rest例子解析
- Post Json 例子 Spring Rest Template & HttpClient
- spring mvc和rest风格小例子
- spring-mvc 的 拦截器应用例子
- jersey rest+spring 以及rest的简介
- cxf spring Rest 风格的在jetty下的heolloworld例子
- .net环境 应用SOAP客户端引用webservice的例子
- 一个REST API 在Azure上应用的简单通讯录例子。(C#, MVC)
- Asp.net之获取GridView控件中TextBox等控件的值
- 全动态代码实现对话框
- 变量的存储
- 让我看了很有感触。
- 用MyEclips写网页文件时,光标不会乱跳了
- spring 3.0.5 release 对REST 客户端的应用例子
- VC2008下提示找不到MSVCP90D.dll的解决办法
- Oracle BI Interactive Dashboards链接错误解决方法
- 用js实现图片展开的效果
- 对MTK模拟器的扩展
- 在WinCe下如何让程序全屏
- UPNP协议细节(转)
- sprintf的使用
- ubuntu分区