1.1 将路径中的数据映射到形参中
/**
* 测试@PathVariable
*
* @param id
* @return
*/
@RequestMapping(value = "/pathVariable/{id}", method = RequestMethod.GET)
@ResponseBody
public Object pathVariable(@PathVariable("id") Integer id) {
return id;
}
请求示例:
/**
* 测试@PathVariable
*/
public static void testPathVariable() {
String url = "http://localhost:81/test/pathVariable/1";
String result = HttpClientUtil.doGet(url, null);
System.out.println(result);
}
1.2 get请求将键值对映射到方法形参中
/**
* 测试get请求@pathParam
*
* @param a
* @param b
* @return
*/
@RequestMapping(value = "/getPathParam", method = RequestMethod.GET)
@ResponseBody
public Object requestParamGet(@PathParam("a") String a, @PathParam("b") String b) {
return a + b;
}
示例请求:
public static void testDoGetPathParam() {
// 参数信息
Map<String, Object> param = new HashMap<String, Object>();
param.put("a", "a");
param.put("b", "b");
String url = "http://localhost:80/test/getPathParam";
String result = HttpKitUtil.doGet(url, param);
System.out.println(result);
}
2 将键值对映射到Map中
/**
* 测试@RequestParam
*
* @param param
* @return
*/
@RequestMapping(value = "requestParam", method = RequestMethod.POST)
@ResponseBody
public Object requestParam(@RequestParam Map<String, Object> param) {
return param;
}
请求示例:
/**
* 测试@RequestParam
*/
public static void testRequestParam() {
String url = "http://localhost:81/test/requestParam";
Map<String, String> param = new HashMap<String, String>();
param.put("a", "1");
param.put("b", "2");
param.put("c", "3");
System.out.println(HttpClientUtil.doPostForm(url, param));
}
3 将JSON数据映射到对象中
/**
* 测试对象映射
*
* @param user
* @return
*/
@RequestMapping(value = "user", method = RequestMethod.POST)
@ResponseBody
public Object user(@RequestBody User user) {
return user;
}
User类
public class User {
private Integer id;
private String name;
private String password;
//省略get和set
请求示例:
/**
* 测试@RequestBody
*/
public static void testRequestBody() {
String url = "http://localhost:81/test/requestBody";
Map<String, String> param = new HashMap<String, String>();
param.put("a", "1");
param.put("b", "2");
param.put("c", "3");
String strParam = JSONObject.toJSON(param).toString();
System.out.println(HttpClientUtil.doPostJson(url, strParam));
}
将Content-Type为application/json,发送json数据映射到Map中:
@ResponseBody
@RequestMapping(value = "/checkEndExpireDate", method = RequestMethod.POST)
public Object checkContractExpire(HttpServletRequest request, HttpServletResponse response,
@RequestBody Map<String, Object> mapParams) {
注意:
@RequestParam:适用于发送头ContentType为application/x-www-form-urlencoded,将键值对数据映射到Map中。
@RequestBody:适用于发送头ContentType不为application/x-www-form-urlencoded,将键值对数据映射到Map中
参考:http://blog.csdn.net/kobejayandy/article/details/12690161
4 将键值对映射到实例对象中:
//测试对象映射
@RequestMapping(value = "user", method = RequestMethod.POST)
@ResponseBody
public Object user(@RequestBody User user) {
return user;
}
请求示例:
//测试映射到对象
public static void testUser() {
String url = "http://localhost:81/test/user";
JSONObject obj = new JSONObject();
obj.put("id", 1);
obj.put("name", "name");
obj.put("password", "password");
String str = obj.toJSONString();
System.out.println(HttpClientUtil.doPostJson(url, str));
}
5 从Request中获取JSON数据
//测试从Request中获取JSON数据
@RequestMapping(value = "getDataFromRequest", method = RequestMethod.POST)
@ResponseBody
public Object getDataFromRequest(HttpServletRequest request,
HttpServletResponse response) {
String result = "";
try {
String inputLine;
while ((inputLine = request.getReader().readLine()) != null) {
result += inputLine;
}
request.getReader().close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
请求示例:
//测试从Request中获取数据
public static void testGetDataFromRequest() {
String url = "http://localhost:81/test/getDataFromRequest";
Map<String, String> param = new HashMap<String, String>();
param.put("a", "1");
param.put("b", "2");
param.put("c", "3");
String strParam = JSONObject.toJSON(param).toString();
System.out.println(HttpClientUtil.doPostJson(url, strParam));
}
6 传递XML数据
/**
* 测试从Request中获取数据
*
* @param user
* @return
*/
@RequestMapping(value = "getDataFromRequest", method = RequestMethod.POST)
@ResponseBody
public Object getDataFromRequest(HttpServletRequest request, HttpServletResponse response) {
String notityJson = "";
String inputLine;
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityJson += inputLine;
}
logger.info(notityJson);
@SuppressWarnings("unchecked")
Map<String, Object> param = new XmlMapper().readValue(notityJson, HashMap.class);//xml转为Map
logger.info(param.toString());
request.getReader().close();
} catch (Exception e) {
e.printStackTrace();
}
return notityJson;
}
Map转为XML后请求,放回Xml后转为Map。
Map<String, Object> param=new HashMap<String, Object>();
param.put("a", "1");
param.put("b", "2");
param.put("c", "3");
String strParam = convertMapToXml(param);//Map转为XML
System.out.println("Map转为XML后:"+strParam);
String resultXmlData = HttpClientUtil.doPostXml("http://localhost:80/test/getDataFromRequest", convertMapToXml(param));
System.out.println("xml转Map后:"+convertXmlToMap(resultXmlData));//Xml转Map
HttpClientUtil工具类中的静态方法可以参考:http://blog.csdn.net/btwangzhi/article/details/79029350
Map与Xml之间的转换可参考
http://blog.csdn.net/BtWangZhi/article/details/55121815