SpringMVC如何接收json数据
请求头:Content-Type=application/json
数据如:
{"mobile":"12345678912","smsContent":"今天天气很好!!","sendTime":"2017-05-18 22:43:21","addSerial":"106591111"}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jianwu.domain.SmsHttpMo;
import com.jianwu.domain.SmsReceipt;
import com.jianwu.manager.SmsHttpMoManager;
import com.jianwu.manager.SmsReceiptManager;
import com.jianwu.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
@Controller
@RequestMapping("gjapi/sms")
public class SmsReceiptController {
private static final Logger logger = LoggerFactory.getLogger(SmsReceiptController.class);
@Autowired
private SmsReceiptManager smsReceiptManager;
@Autowired
private SmsHttpMoManager smsHttpMoManager;
/**
* @param smsReceiptJSON
*/
@RequestMapping(value = "receipt",method = {RequestMethod.POST})
@ResponseBody
public void receipt(@RequestBody String smsReceiptJSON){
logger.info("---smsReceiptJSON:---" + smsReceiptJSON);
JSONObject json = JSON.parseObject(smsReceiptJSON);
SmsReceipt smsReceipt = new SmsReceipt();
if(json.containsKey("reportStatus")){
smsReceipt.setReportStatus(json.getString("reportStatus"));
}
if(json.containsKey("mobile")){
smsReceipt.setMobile(json.getString("mobile"));
}
if(json.containsKey("submitDate")){
smsReceipt.setSubmitDate(DateUtils.stringToDate(json.getString("submitDate"), "yyyyMMddHHmmss"));
}
if(json.containsKey("receiveDate")){
smsReceipt.setReceiveDate(DateUtils.stringToDate(json.getString("receiveDate"), "yyyyMMddHHmmss"));
}
if(json.containsKey("errorCode")){
smsReceipt.setErrorCode(json.getString("errorCode"));
}
if(json.containsKey("msgGroup")){
smsReceipt.setMsgGroup(json.getString("msgGroup"));
}
logger.info("---smsReceipt:---" + smsReceipt.toString());
smsReceiptManager.insert(smsReceipt);
}
/**
* @param httpMoJSON
*/
@RequestMapping(value = "httpMo",method = {RequestMethod.POST})
@ResponseBody
public void httpMo(@RequestBody String httpMoJSON){
logger.info("---httpMoJSON:---" + httpMoJSON);
JSONObject json = JSON.parseObject(httpMoJSON);
SmsHttpMo smsHttpMo = new SmsHttpMo();
if(json.containsKey("mobile")){
smsHttpMo.setMobile(json.getString("mobile"));
}
if(json.containsKey("smsContent")){
smsHttpMo.setSmsContent(json.getString("smsContent"));
}
if(json.containsKey("sendTime")){
smsHttpMo.setSendTime(DateUtils.stringToDate(json.getString("sendTime"), "yyyy-MM-dd HH:mm:ss"));
}
if(json.containsKey("addSerial")){
smsHttpMo.setAddSerial(json.getString("addSerial"));
}
logger.info("---smsHttpMo:---" + smsHttpMo.toString());
smsHttpMoManager.insert(smsHttpMo);
}
}
不积跬步无以至千里,不积小流无以成江海