- SpringMVC中JSON自动转化对象
-在进行api开发时,通过ajax在controller层直接传入一个对象方便又实用,但是细节方面却有很多技巧。
昨天晚上遇到的问题:controller能够识别到参数,但是传入的值全部为空,在网上找了很久,一般的解决办法是设置传入content的类型为application/json,导入fastjson的两个包,然鹅,这样并没有解决我的问题。最后自己设置成x-www-form-urlencode解决了。现在整理一下,给出自动转化对象的解决方案。
-entity对象:
public class QueryString {
String vcn;
Integer vol;
String spd;
String textPut;
String textType;
@Override
public String toString() {
return "QueryString{" +
"vcn='" + vcn + '\'' +
",vol=" + vol +
",spd='" + spd + '\'' +
",textPut='" + textPut + '\'' +
",textType='" + textType + '\'' +
'}';
}
public String getVcn() {
return vcn;
}
public QueryString setVcn(String vcn) {
this.vcn = vcn;
return this;
}
public Integer getVol() {
return vol;
}
public QueryString setVol(Integer vol) {
this.vol = vol;
return this;
}
public String getSpd() {
return spd;
}
public QueryString setSpd(String spd) {
this.spd = spd;
return this;
}
public String getTextPut() {
return textPut;
}
public QueryString setTextPut(String textPut) {
this.textPut = textPut;
return this;
}
public String getTextType() {
return textType;
}
public QueryString setTextType(String textType) {
this.textType = textType;
return this;
}
}
Controller层:
@ResponseBody
@RequestMapping(value = "/api/voice",method = RequestMethod.POST,produces =
MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"
)
public String voice(QueryString queryString) throws Exception {
logger.debug(queryString.toString());
try {
System.out.println(queryString.toString());
Result result = voiceService.getVoice(queryString);
return JSON.toJSONString(result);
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
Ajax提交的方法:
$.ajax({
type: 'post',url: "/api/voice",data: {"vcn": vcn,"vol": vol,"spd": spd,"textPut": textPut,"textType": textType},contentType: "application/x-www-form-urlencoded;charset=utf-8",success: function (res) {
if (0 == res["status"]) {
music = res['data'][0];
$("#urls").html("<a>" + music + "</a>")
$("#show_play").removeAttr('hidden');
$("#show_play button").on('click',function () {
window.location.href = music;
});
} else {
layer.msg(res["msg"]);
}
$("#showTooltips").attr('class',"weui-btn weui-btn_primary");
},error: function (res) {
layer.msg("错误码:" + res.status);
$("#showTooltips").attr('class',"weui-btn weui-btn_primary");
}
});
最好在WEB-INF/web.xml中设置一下乱码过滤器:
<!--spring为我们提供的乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样就基本OK,ajax传入的是一个application/x-www-form-urlencode类型的对象,注意这里不是application/json,也没有通过JSON.stringify()格式化,在controller层中,我们通过直接对方法传入对象,获取到的request的数据的格式是通过RequestMapping指定的:
produces = MediaType.APPLICATION_JSON_VALUE + “;charset=UTF-8”
这样我们就可以通过Springmvc自动转化json参数为对象了。
注: maven中的Spring相关的依赖都是4.3.x
原文链接:https://www.f2er.com/ajax/160484.html