美文网首页
SpringMVC 参数绑定 后端json接收参数

SpringMVC 参数绑定 后端json接收参数

作者: young_____ | 来源:发表于2018-12-20 17:24 被阅读0次

SpringMVC旧的写法

在SpringMVC项目中,通常是用controller层来接受前端传过来的数据,但是我们目前都是用JSONObject来接收值,需要多做一步解析jsonObject的动作 例子如下:

$.ajax({
    url:"<%=contextPath%>/entry/saveOrUpdateEntry",
    type:'POST',
    data:nui.encode(json),
    cache:false,
    contentType:'application/json',
    success:function(text){
    }
});

@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody JSONObject jsonObject , HttpSession session) {
           ·····

优化写法

一个参数对象情况

json = {"empCode":"80489179"}

$.ajax({
    url:"<%=contextPath%>/entry/saveOrUpdateEntry",
    type:'POST',
    data:nui.encode(json),
    cache:false,
    contentType:'application/json',
    success:function(text){
        
    }
});

@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(String empCode , HttpSession session) {
           ·····

一个bean的情况

json = {"empCode":"80489179","empName":"李明”,“age":"15"}

$.ajax({
    url:"<%=contextPath%>/entry/saveOrUpdateEntry",
    type:'POST',
    data:nui.encode(json),
    cache:false,
    contentType:'application/json',
    success:function(text){
        
    }
});

@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody User user, HttpSession session) {
     
 class User{
     private String empCode;
     private String empName;
     private Integer age;
     ···
 }              

一个Bean+一个list情况

json = {"empCode":"80489179","empName":"李明","age":"15","list":[{"id":"1","name":"小啊"},{"id":"2","name":"小明"}]}

$.ajax({
    url:"<%=contextPath%>/entry/saveOrUpdateEntry",
    type:'POST',
    data:nui.encode(json),
    cache:false,
    contentType:'application/json',
    success:function(text){
        
    }
});

@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody EmpVo empvo, HttpSession session) {
     
 class EmpVo{
     private String empCode;
     private String empName;
     private Integer age;
     private List<Dict> list;
     。。。
     。。。
     。。
 }      
 class Dict{
     private Integer id;
     private String name;
 }

多个Bean+一个list情况

json = {"user":{"empCode":"80489179","empName":"李明”,"age":"15"},"list":[{"id":"1","name":"小啊"},{"id":"2","name":"小明"}],"like":{"hobby":"我的爱好","spcial":"没有系好"}}

$.ajax({
    url:"<%=contextPath%>/entry/saveOrUpdateEntry",
    type:'POST',
    data:nui.encode(json),
    cache:false,
    contentType:'application/json',
    success:function(text){
        
    }
});

@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody Vo vo, HttpSession session) {
     
 class Vo{
     private User user;
     private Like like;
     private List<Dict> list;
     ···
 }      
 class Dict{
     private Integer id;
     private String name;
 }
 class Like{
     private String hobby;
     private String spcial;
 }
 class User{
     private String empCode;
     private String empName;
     private Integer age;
     ···
 }        
    

相关文章

网友评论

      本文标题:SpringMVC 参数绑定 后端json接收参数

      本文链接:https://www.haomeiwen.com/subject/jwxjvftx.html