@RequestMapping("/user_systemallot_insert.do")
@ResponseBody
public Map<String, Object> systemallotInsert(HttpServletRequest request,HttpSession session){
Map<String, Object> map = new HashMap<>();
HashMap<String, Object> rlt = new HashMap<>();
HashMap<String, String> params = new HashMap<>();
try {
Object identityCode = session.getAttribute("identity_code");
if(identityCode==null||"".equals(identityCode)){
map.put("status", 2);
map.put("message", "未登录");
return map;
}
rlt.put("product_coding", request.getParameter("productCodes"));
rlt.put("view", request.getParameter("view"));
rlt.put("edit", request.getParameter("edit"));
params.put("identity_code",identityCode.toString());
params.put("mobile", request.getParameter("mobile"));
params.put("truename", request.getParameter("truename"));
params.put("gid", request.getParameter("gid"));
params.put("password", request.getParameter("secondpwd"));
params.put("auth", ConvertMap2Json.buildJsonBody(rlt, 0, true));
String json = HttpClientUtil.httpPost("http://test.shangke.com/app/user/register/", params, "utf-8");
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
if(1==node.findValue("status").intValue()){
map.put("status", 1);
map.put("message", "提交成功");
}else{
map.put("status", 0);
map.put("message", node.findValue("message").asText());
}
} catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
map.put("message", "系统异常");
}
return map;
}
ConvertMap2Json.java类
<!---------------------------------------------开始-------------------------------------->
package com.shangyukeji.icoolcloud.util;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Set;
public class ConvertMap2Json {
private static final String QUOTE = "\"";
/**
* 将Map转成一个json对象,即像大括号的东西
* e.g.
* {
* "eventName": "name"
* }
*
* @param body
* @param tabCount
* @param addComma
* @return
*/
public static String buildJsonBody(Map<String, Object> body, int tabCount, boolean addComma)
{
StringBuilder sbJsonBody = new StringBuilder();
sbJsonBody.append("{\n");
Set<String> keySet = body.keySet();
int count = 0;
int size = keySet.size();
for (String key : keySet)
{
count++;
sbJsonBody.append(buildJsonField(key, body.get(key), tabCount + 1, count != size));
}
sbJsonBody.append(getTab(tabCount));
sbJsonBody.append("}");
return sbJsonBody.toString();
}
/**
* 生成一个json字段即像这样的
* e.g.
* "eventName": "aaa"
*
* @param key
* @param value
* @param tabCount
* @param addComma
* @return
*/
private static String buildJsonField(String key, Object value, int tabCount, boolean addComma)
{
StringBuilder sbJsonField = new StringBuilder();
sbJsonField.append(getTab(tabCount));
sbJsonField.append(QUOTE).append(key).append(QUOTE).append(": ");
sbJsonField.append(buildJsonValue(value, tabCount, addComma));
return sbJsonField.toString();
}
/**
* 生成json值对象
* e.g.
* "string"
* 或
* {
* "key": "value"
* }
*
* @param value
* @param tabCount
* @param addComma
* @return
*/
private static String buildJsonValue(Object value, int tabCount, boolean addComma)
{
StringBuilder sbJsonValue = new StringBuilder();
if (value instanceof String)
{
sbJsonValue.append(QUOTE).append(value).append(QUOTE);
}
else if (value instanceof Integer || value instanceof Long || value instanceof Double)
{
sbJsonValue.append(value);
}
else if (value instanceof java.util.Date)
{
sbJsonValue.append(QUOTE).append(formatDate((java.util.Date) value)).append(QUOTE);
}
else if (value.getClass().isArray() || value instanceof java.util.Collection)
{
sbJsonValue.append(buildJsonArray(value, tabCount, addComma));
}
else if (value instanceof java.util.Map)
{
sbJsonValue.append(buildJsonBody((java.util.Map) value, tabCount, addComma));
}
sbJsonValue.append(buildJsonTail(addComma));
return sbJsonValue.toString();
}
/**
* 生成json数组对象
* [
* "value"
* ]
* 或
* [
* {
* "key": "value"
* }
* ]
*
* @param value
* @param tabCount
* @param addComma
* @return
*/
private static String buildJsonArray(Object value, int tabCount, boolean addComma)
{
StringBuilder sbJsonArray = new StringBuilder();
sbJsonArray.append("[\n");
Object[] objArray = null;
if (value.getClass().isArray())
{
objArray = (Object[]) value;
}
else if (value instanceof java.util.Collection)//将集合类改为对象数组
{
objArray = ((java.util.Collection) value).toArray();
}
int size = objArray.length;
int count = 0;
//循环数组里的每一个对象
for (Object obj : objArray)
{
//加上缩进,比上一行要多一个缩进
sbJsonArray.append(getTab(tabCount + 1));
//加上对象值,比如一个字符串"String",或者一个对象
sbJsonArray.append(buildJsonValue(obj, tabCount + 1, ++count != size));
}
sbJsonArray.append(getTab(tabCount));
sbJsonArray.append("]");
return sbJsonArray.toString();
}
/**
* 加上缩进,即几个\t
* TODO.
*
* @param count
* @return
*/
private static String getTab(int count)
{
StringBuilder sbTab = new StringBuilder();
while (count-- > 0)
{
sbTab.append("\t");
}
return sbTab.toString();
}
/**
* 加上逗号
* TODO.
*
* @param addComma
* @return
*/
private static String buildJsonTail(boolean addComma)
{
return addComma ? ",\n" : "\n";
}
/**
* 格式化日期
* TODO.
*
* @param date
* @return
*/
private static String formatDate(java.util.Date date)
{
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
}
<!----------------------------------结束----------------------------------------->