jsonp 常用Java方法
(1)以jsonp的形式返回:函数名(json字符串)
- /***
- * 用于jsonp调用
- * @param map : 用于构造json数据
- * @param callback : 回调的javascript方法名
- * @param filters : <code>SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
- .serializeAllExcept("content");
- FilterProvider filters = new SimpleFilterProvider().addFilter(
- Constant2.SIMPLEFILTER_JACKSON_PAPERNEWS, theFilter);</code>
- * @return : js函数名(json字符串)
- */
- public static String getJsonP(Object map,String callback,FilterProvider filters)
- {
- if(ValueWidget.isNullOrEmpty(map)){
- return null;
- }
- String content = null;
- if(map instanceof String){
- content=(String)map;
- }else{
- ObjectMapper mapper = getObjectMapper();
- ObjectWriter writer=null;
- try {
- if(filters!=null){
- writer=mapper.writer(filters);
- }else{
- writer=mapper.writer();
- }
- content=writer.writeValueAsString(map);
- logger.info(content);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(ValueWidget.isNullOrEmpty(callback)){
- return content;
- }
- return callback+"("+content+");";
- }
应用:
- @ResponseBody
- @RequestMapping(value = "/json_detail", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String jsonDetail(Model model, int id/*新闻的id*/,
- PaperNewsView view, HttpSession session,
- HttpServletRequest request, String callback) throws IOException {
- init(request);
- PaperNews news=(PaperNews) getDao().get(id);
- String content;
- /*int comment_type=0;
- if(type==2){//新闻
- comment_type=Constant2.COMMENT_TARGET_TYPE_NEWS;
- }else if(type==1){
- }*/
- String title=null;
- if(news.getType()==Constant2.TYPE_NEWS){
- title="新闻";
- }else{
- title="报料";
- }
- long commentCount=this.newsCommentDao.getCount(null, id, 1);
- news.setCommentSum(commentCount);
- if(!ValueWidget.isNullOrEmpty(news.getPic())){
- news.setPic(JSONPUtil.getPicPath(news.getPic()));
- }
- content = getJsonP(news, callback);
- AccessLog accessLog=logInto(request);
- accessLog.setDescription("手机端"+title+"详情,id:"+id);
- logSave(accessLog, request);
- return content;
- }
(2)去掉callback
callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"}
- /***
- * callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"}
- * @param input
- * @param callbackName
- * @return
- */
- public static String deleteCallback(String input,String callbackName){
- return input.replaceAll("^"+callbackName+"\\((.*\\})\\);?$", "$1");
- }
应用:
- /***
- * convert json string to Map;e.g:{errorMessage=系统异常,请稍后再试!, id=, status=02, errorCode=error_default, method=}
- * @param jsonResult
- * @return
- * @throws UnsupportedEncodingException
- * @throws JSONException
- * @throws org.json.JSONException
- */
- public static Map<String, String> getMap(String jsonResult)
- throws UnsupportedEncodingException, JSONException,
- org.json.JSONException {
- if(ValueWidget.isNullOrEmpty(jsonResult)){
- return null;
- }
- //callback({"auth_code":"v39hXq"})
- jsonResult=deleteCallback(jsonResult, "callback");
- Map<String, String> resultMap =null;
- Map obj = (Map) JsonParser.parserRandomJsonFormat(jsonResult);
- if (ValueWidget.isNullOrEmpty(obj)) {
- return null;
- }
- List resultList = (List) obj.get("resultList");
- if(ValueWidget.isNullOrEmpty(resultList)){
- resultMap=obj;
- }else{
- resultMap= new HashMap<String, String>();
- for (int i = 0; i < resultList.size(); i++) {
- Map mp_tmp = (Map) resultList.get(i);
- parseMap(resultMap, mp_tmp);
- }
- }
- return resultMap;
- }
参考:http://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html