文章源自JAVA秀-https://www.javaxiu.com/21244.html 闪耀的瞬间 收藏 分类专栏: 版权 文章源自JAVA秀-https://www.javaxiu.com/21244.html 文章源自JAVA秀-https://www.javaxiu.com/21244.html 往往有这么个需求,在新增用户时,页面以 ajax 方式把 user 对象数据提交到后台控制器上, 控制器方法中的参数以对象形式进行接收,这时就需要把json字符串自动转换为对象文章源自JAVA秀-https://www.javaxiu.com/21244.html 在SpringBoot或SpringMvc中,有@ResponseBody 、@RequestBody 两个注解文章源自JAVA秀-https://www.javaxiu.com/21244.html 通过继承 WebMvcConfigurationSupport 类,重写configureMessageConverters方法,在项目中引用 fastjson库,它有一个FastJsonHttpMessageConverter的类,实现了HttpMessageConverter消息转换,我们只要设置支持转换那些类型即可,设置如下:文章源自JAVA秀-https://www.javaxiu.com/21244.html ajax提交字符串数据,contentType: ‘application/json’文章源自JAVA秀-https://www.javaxiu.com/21244.html 控制器接收,consumes = “application/json”文章源自JAVA秀-https://www.javaxiu.com/21244.html method 表示接收 POST方式的请求 consumes="application/json"表示只处理请求头中Content-Type为application/json的请求 produces=“application/json” 表示向客户端返回Content-Type为application/json的数据文章源自JAVA秀-https://www.javaxiu.com/21244.html 看一下调式效果,可以看出对象中的值,ajax过来的json字符串数据,已经自动转换为对象了 文章源自JAVA秀-https://www.javaxiu.com/21244.htmlSpringBoot项目开发(二十三):Ajax Post数据到控制器方法,参数自动转换为对象
文章源自JAVA秀-https://www.javaxiu.com/21244.html文章源自JAVA秀-https://www.javaxiu.com/21244.html
2018-10-18 19:51:41
1901
文章源自JAVA秀-https://www.javaxiu.com/21244.html
4
spring boot
java
SpringBoot 项目开发 文章源自JAVA秀-https://www.javaxiu.com/21244.html@Configuration
public class CustomeInterceptor extends WebMvcConfigurationSupport {
//UTF-8 格式的数据
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XML);
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
supportedMediaTypes.add(MediaType.TEXT_XML);
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(fastConverter);
}
}
1234567891011121314151617181920212223<script type="text/javascript" th:inline="javascript">
function saveMenu() {
var data = JSON.stringify({id: "1",pId : "root",name:"菜单"})
$.ajax({
type : "post",
url : "/saveMenu",
data : data,
contentType: 'application/json',
success : function (result) {
console.log(result);
}
})
}
</script>
1234567891011121314 @RequestMapping(value = "/saveMenu" , method = RequestMethod.POST , consumes = "application/json")
@ResponseBody
public String saveMenu(@RequestBody Tree tree){
System.out.println("tree:" + JSON.toJSONString(tree));
return "success";
}
123456文章源自JAVA秀-https://www.javaxiu.com/21244.html

评论