使用@requestbody接收JSON数据

来源:互联网 发布:防止网络诈骗图片 编辑:程序博客网 时间:2024/05/04 05:33
首先,使用普通的String接收:
//使用String接收JSON方法    @RequestMapping(value="/addECMurl",            method = RequestMethod.POST/*, consumes = {"application/json"}, produces = {"application/json"}*/)    public @ResponseBody    ModelAndView addECM1(String typeid, HttpSession session, HttpServletRequest request,                         @RequestBody String s) {        List<TypeBrief> types = farmDocTypeManagerImpl.getTypesForWriteDoc(getCurrentUser(session));/*String s="{\n" +                "  \"fileTitle\": \"ECM推送\",\n" +                "  \"fileurl\": \"www.baidu.com\",\n" +                "  \"docid\": \"kjdlsk244s45das87sa8d7s8dasa\"\n" +                "}";*/JSONObject ecm=JSONObject.fromObject(s);String fileTitle=ecm.getString("fileTitle");String fileurl=ecm.getString("fileUrl");String docid=ecm.getString("docId");          // 回显分类        DocEntire doce = new DocEntire();        FarmDoctype doctype = farmDocTypeManagerImpl.getType(typeid);        doce.setType(doctype);        // 回显小组        Doc doc = new Doc();        doc.setTitle(fileTitle);        doc.setId(docid);        doc.setSource(fileurl);        doce.setDoc(doc);        return ViewMode.getInstance().putAttr("types", types).putAttr("doce", doce)                .returnModelAndView(ThemesUtil.getThemePath() + "/know/ECMcreat");    }

然后使用对象再来接收一遍:

先创建一个实体类:

import java.io.Serializable;/** * Created by HEHE on 2016/10/9. */public class ECM implements Serializable {    private static final long serialVersionUID = 3170304648147893712L;    public ECM() {    }    private String fileTitle;    private String fileUrl;    private String docId;    public String getFileTitle() {        return fileTitle;    }    public void setFileTitle(String fileTitle) {        this.fileTitle = fileTitle;    }    public String getFileUrl() {        return fileUrl;    }    public void setFileUrl(String fileUrl) {        this.fileUrl = fileUrl;    }    public String getDocId() {        return docId;    }    public void setDocId(String docId) {        this.docId = docId;    }}
将JSON中的变量名一一对应,并且注意发送json的一端要在  发送头中设置application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。

  @RequestMapping(value="/addECMu",            method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})    public @ResponseBody    ModelAndView addECM11(String typeid, HttpSession session, HttpServletRequest request,                         @RequestBody ECM ecmurl) {        List<TypeBrief> types = farmDocTypeManagerImpl.getTypesForWriteDoc(getCurrentUser(session));       /* String s="{\n" +                "  \"fileTitle\": \"ECM推送\",\n" +                "  \"fileUrl\": \"www.baidu.com\",\n" +                "  \"docId\": \"kjdlsk244s45das87sa8d7s8dasa\"\n" +                "}";*/        String fileTitle = ecmurl.getFileTitle();        String fileurl = ecmurl.getFileUrl();        String docid = ecmurl.getDocId();        // 回显分类        DocEntire doce = new DocEntire();        FarmDoctype doctype = farmDocTypeManagerImpl.getType(typeid);        doce.setType(doctype);        // 回显小组        Doc doc = new Doc();        doc.setTitle(fileTitle);        doc.setId(docid);        doc.setSource(fileurl);        doce.setDoc(doc);        return ViewMode.getInstance().putAttr("types", types).putAttr("doce", doce)                .returnModelAndView(ThemesUtil.getThemePath() + "/know/ECMcreat");    }



还有最最细节的一步就是 在Spring-MVC配置文件中设置

<mvc:annotation-driven/>


0 0
原创粉丝点击