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", "zh@.com");
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();
}


}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值