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 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 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有长度限制。

你可能感兴趣的