工做中經常使用到一些接口要調用其餘項目的接口,以達到數據互通的目的,今天寫了一個響應的,想記錄一下,之後沒事翻翻。固然了接口響應確定不止這一種形式,還有其餘不少。java
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(); Map<String,String> map = new HashMap<String,String>(); 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<String,String> map = new HashMap<String,String>(); map.put("code", "0"); map.put("msg", "失敗"); out1.print(JSONSerializer.toJSON(map)); out1.flush(); out1.close(); } catch (Exception e1) { e1.printStackTrace(); } } return null; } }