Spring MVC 4.1+Mybatis ajax 异步返回Json 415问题解决方案

1.首先需要配置好开发环境,这个十分重要,后续工作全部依赖于此。具体配置方法如下:
在applicationContext.xml配制文件中添加如下代码

 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  <property name="messageConverters">
   <list >
    <ref bean="mappingJacksonHttpMessageConverter" />
   </list>
  </property>
 </bean>
 <bean id="mappingJacksonHttpMessageConverter"
  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>text/html;charset=UTF-8</value>
   </list>
  </property>
 </bean> 

2 导入需要使用的Jacson 2.5.1 相关jar 包3个:
jackson-annotations-2.5.1.jar
jackson-core-2.5.1.jar
jackson-databind-2.5.1.jar
下载地址:http://download.csdn.net/detail/hao316068545/9064785

3在Controller 中添加如下代码

@RequestMapping(value = "/pages/affiche/modifyNotice.ajax" ) 
    public void modifyNotice(@RequestBody TAffiche notice, HttpServletRequest request, HttpServletResponse response)  throws IOException{

        int id         = notice.getId();
        String title   = notice.getTitle();
        String content = notice.getContent();
        long time      = notice.getTime();
        String editor  = notice.getEditor();
        int status     = notice.getStatus();
        int type       = notice.getType();

        int flag = afficheService.updateAfficheData(id, title, content, time, editor, status, type);
        Map<String, Object> map = new HashMap<>();
        map.put("flag", flag);
        String json = JSONArray.toJSONString(map);
        logger.info("获取更新返回值:"+json);
        WebUtil.reponseJson(json, response);
    }

4前台部分代码示例如下
需要传递到后台的参数组

     var notice  = { 
         "id"      : parseInt($("#ds_id_m").val()),
         "title"   : $("#ds_title_m").val(),
         "content" : $("#ds_content_m").val(),
         "time"    : dateTime,
         "editor"  : $("#ds_username_m").val(),
         "status"  : 1,
         "type"    : parseInt($("#modifySelect").val())
         }; 

对应的ajax 代码如下,需要注意要用JSON.stringify(notice)转换为字符串进行传输。

$.ajax({type:"POST",                              url:"/JK_AirPortPro/pages/affiche/modifyNotice.ajax",
dataType:"JSON",                         contentType:'application/JSON;charset=UTF-8', 
data:JSON.stringify(notice), 
success:function(data){  
         if(data.flag==1){
             $("#modify-success").alert();
         }else{
              console.log(data.flag);
              $("#modify-fail").alert();
         }
       },  
    error:function(data){  
    if(data.flag==0){
    console.log(data.flag);
    $("#modify-fail").show();   
     } 
    }  
}); 

5 Controller 用来接收参数的java bean 如下:
代码可由Mybatis自动生成,也可以自己写,重点是需要添加无参构造方法。

public class TAffiche {

    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.id
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private Integer id;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.title
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private String title;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.content
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private String content;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.time
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private Long time;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.editor
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private String editor;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.status
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private Integer status;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column t_affiche.type
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    private Integer type;

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.id
     * @return  the value of t_affiche.id
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public Integer getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.id
     * @param id  the value for t_affiche.id
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.title
     * @return  the value of t_affiche.title
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public String getTitle() {
        return title;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.title
     * @param title  the value for t_affiche.title
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.content
     * @return  the value of t_affiche.content
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public String getContent() {
        return content;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.content
     * @param content  the value for t_affiche.content
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.time
     * @return  the value of t_affiche.time
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public Long getTime() {
        return time;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.time
     * @param time  the value for t_affiche.time
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setTime(Long time) {
        this.time = time;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.editor
     * @return  the value of t_affiche.editor
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public String getEditor() {
        return editor;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.editor
     * @param editor  the value for t_affiche.editor
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setEditor(String editor) {
        this.editor = editor == null ? null : editor.trim();
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.status
     * @return  the value of t_affiche.status
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public Integer getStatus() {
        return status;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.status
     * @param status  the value for t_affiche.status
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column t_affiche.type
     * @return  the value of t_affiche.type
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public Integer getType() {
        return type;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column t_affiche.type
     * @param type  the value for t_affiche.type
     * @mbggenerated  Wed Aug 26 09:06:33 CST 2015
     */
    public void setType(Integer type) {
        this.type = type;
    }

    public TAffiche(Integer id, String title, String content, Long time,
            String editor, Integer status, Integer type) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.time = time;
        this.editor = editor;
        this.status = status;
        this.type = type;
    }
    public TAffiche() {

    }
}

6对于遇到的415问题:
主要是Spring 要与jackson版本对应,Spring 4.1需要使用jackson2.5.1,
其次需要注意指定传输类型contentType:’application/JSON;charset=UTF-8’,

7本文参考:
配置SpringMVC返回JSON遇到的坑
http://blog.csdn.net/caiwenfeng_for_23/article/details/43492973

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值