spring restTemplate 支持返回近json和xml

servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
       
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.spring.mvcrest.web" />
    <bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
          <property name="supportedMediaTypes" value="application/xml"/>
    </bean>
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
       <property name="supportedMediaTypes" value="application/json" />
    </bean>
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
          <list>
            <value>com.spring.mvcrest.bean.Employee</value>
            <value>com.spring.mvcrest.bean.EmployeeList</value>
          </list>
        </property>
    </bean>
   
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
       <property name="messageConverters">
           <list>
               <ref bean="jsonConverter" />
               <ref bean="marshallingConverter" />
           </list>
       </property>
     </bean>

</beans>



RestClient.java

package com.spring.mvcrest.client;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import com.spring.mvcrest.util.CommonsUtil;

public class RestClient {
  
    private static final String preUrl = "http://localhost:8080/spring-mvc-rest/rest/";
    private static RestTemplate restTemplate = new RestTemplate();
   
    /** Add an item*/
    public static Object postJson( Map<String, String> map, String url, String dataType) {
          url =  preUrl + url;
          HttpHeaders requestHeaders = new HttpHeaders();
          List<MediaType> mediaTypes = new ArrayList<MediaType>();
          mediaTypes.add(CommonsUtil.converDataType(dataType));
          requestHeaders.setAccept(mediaTypes);
          HttpEntity<?> requestEntity = new HttpEntity<Object>(map, requestHeaders);
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate();
          return restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody();
    }
   
    /** Update An item
     */
    public static boolean putJson(Map<String, String> map, String url){
        url =  preUrl + url;
        HttpHeaders headers = new HttpHeaders();
        List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
        acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
        headers.setAccept(acceptableMediaTypes);
       
        // HttpEntity<String> doesn't work, we will use map and headers. map is not String.
        HttpEntity<Object> entity = new HttpEntity<Object>(map, headers);
        restTemplate.put(url, entity);
        return true;
    }
   
    public static Object getjson(String url, String id, String dataType) {
       
        url = preUrl + url + id;
        HttpHeaders requestHeaders = new HttpHeaders();
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(CommonsUtil.converDataType(dataType));
        requestHeaders.setAccept(mediaTypes);
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
        // Create a new RestTemplate instance
        RestTemplate restTemplate = new RestTemplate();
     // Make the HTTP GET request to the Basic Auth protected URL
        return restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class).getBody();
           
        
    }
   
    public static boolean deleteJson(String url, String id) {
      
        url = preUrl + url + id;
        // restTemplate.delete(URL);
        restTemplate.exchange(url, HttpMethod.DELETE, null, String.class);
        return true;
    }
   
  
}


Junit test:

package com.util.test;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.spring.mvcrest.client.RestClient;

public class TestRestClient {
   
    @Test
    public void postJsonTest() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "1");
        map.put("name", "2213");
        map.put("email", "123");
        System.out.println(RestClient.postJson(map, "employee", "json"));
       
        map.put("id", "3");
        map.put("name", "JW");
        map.put("email", "sw.com");
        System.out.println(RestClient.postJson(map, "employee", "xml"));
    }
   
    @Test
    public void getJsonTest() {
       Object object = RestClient.getjson("employee/", "1", "xml");
       System.out.println(object);
    }
   
    @Test
    public void putJsonTest() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "2");
        map.put("name", "ze");
        map.put("email", "[email protected]");
        System.out.println(RestClient.putJson(map, "employee"));
       
        getAllJsonTest();
    }
    
   
    @Test
    public void getAllJsonTest(){
      
       System.out.println(RestClient.getjson("employees", "", "xml"));
    }
   
    @Test
    public void deleteJsonTest(){
       getAllJsonTest();
       System.out.println(RestClient.deleteJson("employee/", "2"));
       getAllJsonTest();
    }
   
   
}

猜你喜欢

转载自729583602-qq-com.iteye.com/blog/1912280
今日推荐