乐趣区

关于javascript:Spring-Controller-接收前端JSON数据

POST 申请形式

1. 应用实体类接管

const http = require('http');
const postData = JSON.stringify({
  "id": 1,
  "name": "三体",
  "price": 180
});
const options = {
  hostname: 'localhost',
  port: 8080,
  path: '/ReceiveJsonController/receiveJson1',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
}

const req = http.request(options, res => {console.log(` 状态码: ${res.statusCode}`)

  res.on('data', d => {process.stdout.write(d)
  })
})

req.on('error', error => {console.error(error)
})

req.write(postData)

req.end()
@PostMapping("/receiveJson1")
public void receiveJson1(@RequestBody Book book, HttpServletRequest httpServletRequest) {logger.info("申请形式:" + httpServletRequest.getMethod());
    logger.info("数据:" + book);
}

2. 应用 List 实体类接管

const http = require('http');

const postData = JSON.stringify([{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
},{
  "id": 1,
  "name": "三体",
  "price": 180
}]);


const options = {
  hostname: 'localhost',
  port: 8080,
  path: '/ReceiveJsonController/receiveJson2',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
}

const req = http.request(options, res => {console.log(` 状态码: ${res.statusCode}`)

  res.on('data', d => {process.stdout.write(d)
  })
})

req.on('error', error => {console.error(error)
})

req.write(postData)

req.end()
@PostMapping("/receiveJson2")
public void receiveJson2(@RequestBody List<Book> books, HttpServletRequest httpServletRequest) {logger.info("申请形式:" + httpServletRequest.getMethod());
    logger.info("数据:" + books);
}

3. 应用 Map 接管

const http = require('http');

const postData = JSON.stringify({
  "data": {
    "id": 1,
    "name": "三体",
    "price": 180
  }
});


const options = {
  hostname: 'localhost',
  port: 8080,
  path: '/ReceiveJsonController/receiveJson3',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
}

const req = http.request(options, res => {console.log(` 状态码: ${res.statusCode}`)

  res.on('data', d => {process.stdout.write(d)
  })
})

req.on('error', error => {console.error(error)
})

req.write(postData)

req.end()
@PostMapping("/receiveJson3")
public void receiveJson3(@RequestBody Map<String, Object> paramsMap, HttpServletRequest httpServletRequest) {logger.info("申请形式:" + httpServletRequest.getMethod());
    logger.info("数据:" + paramsMap);
}

应用 Map 接管,留神是 Key:Value 模式

// 单个对象
{
  "data": {
    "id": 1,
    "name": "三体",
    "price": 180
  }
}

// 多个对象
{
    "data": [{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    },{
      "id": 1,
      "name": "三体",
      "price": 180
    }]
}

Get 申请形式

get 申请形式须要留神 encodeURIComponent() 本义,本义后数据为一串字符串,所以只能应用 String 接管,再应用 Java json 工具类转换成对应的对象

const http = require('http')
const options = {
  hostname: 'localhost',
  port: 8080,
  path: '/ReceiveJsonController/receiveJson5',
  method: 'GET'
}

let data = {
  "id": 1,
  "name": "三体",
  "price": 180
};
let jsonString = "?data=" + encodeURIComponent(JSON.stringify(data));
options.path = options.path + jsonString

const req = http.request(options, res => {console.log(` 状态码: ${res.statusCode}`)

  res.on('data', d => {process.stdout.write(d)
  })
})

req.on('error', error => {console.error(error)
})

req.end()
@GetMapping("/receiveJson5")
public void receiveJson5(@RequestParam("data") String data, HttpServletRequest httpServletRequest) {logger.info("申请形式:" + httpServletRequest.getMethod());
    logger.info("数据:" + data);
}

总结

应用 post 申请传递 json 仍然是最好的形式,用 get 也不是不能够,然而 get 有长度限度。

退出移动版