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());

  }

}

原创粉丝点击