工作中常用到一些接口要调用其他项目的接口,以达到数据互通的目的,今天写了一个响应的,想记录一下,以后没事翻翻。当然了接口响应肯定不止这一种形式,还有其他很多。
import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONSerializer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.minxinloan.heWarrFilesCtrl.service.HeWarrFilesService;import com.minxinloan.standardPush.controller.StandardPushCtrl;/** * * @author reynold-白 * */@Controller@Scope("prototype")@RequestMapping(value="/HeWarrFilesCtrl")public class HeWarrFilesCtrl { private static Logger logger = LoggerFactory.getLogger(StandardPushCtrl.class); @Autowired private HeWarrFilesService heWarrFilesService; @RequestMapping(value="/heWarrFiles",method=RequestMethod.POST) @ResponseBody public String heWarrFiles(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("param")String jsonStr){ try{ logger.info("获取【他项权证】json字符串:"+jsonStr); heWarrFilesService.insertHeWarrFiles(jsonStr); logger.info("给金盾响应"); response.setContentType("application/Json;charset=UTF-8"); PrintWriter out = response.getWriter(); Mapmap = new HashMap (); map.put("code", "1"); map.put("msg", "成功"); out.print(JSONSerializer.toJSON(map)); out.flush(); out.close(); }catch(Exception e){ logger.error("【他项权证】入库失败" + e.getMessage()); e.printStackTrace(); try { response.setContentType("application/Json;charset=UTF-8"); PrintWriter out1 = response.getWriter(); Map map = new HashMap (); map.put("code", "0"); map.put("msg", "失败"); out1.print(JSONSerializer.toJSON(map)); out1.flush(); out1.close(); } catch (Exception e1) { e1.printStackTrace(); } } return null; } }