Watch 1 Star 1 Fork 1

Candyメ奶糖 / data-interactionJava

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
前后端交互的方式的演示代码 spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

博客链接

点我学习:前后端交互的方式整理

数据交互格式

可以通过GET/POST等方式将变量、对象、数组、JSON、XML、file/img、base64图片、cookies等各种形式的数据提交到后端。

GET

代码示例:

/**
 * GET-变量
 */
@GetMapping("/testVar1")
public String testVar1(/*@RequestParam("param")*/String param) {
    return param;
}

/**
 * GET-path变量
 */
@GetMapping("/testVar2/{param}")
public String testVar2(@PathVariable/*("param")*/ String param) {
    return param;
}

/**
 * GET-对象
 */
@GetMapping("/testObject1")
public GetParam testObject1(GetParam obj) {
    return obj;
}

/**
 * GET-数组
 */
@GetMapping("/testArray1")
public Object testArray1(String[] obj) {
    return obj;
}

运行结果:

POST

POST方式可以使用curl、postman、getman、eolinker等工具实现。

/**
 * POST-多个变量、对象(form提交)
 */
@PostMapping(value = "/testPostFormVar")
public String testPostVar1(@RequestParam(name = "username") String username,
                          @RequestParam(name = "password") String password) {
    return username + ":" + password;
}

/**
 * POST-JSON(ajax)
 */
@PostMapping(value = "/testPostJsonVar")
public PostParam testPostVar2(@RequestBody PostParam param) {
    return param;
}

/**
 * 通用的post接收方式
 */
@PostMapping(value = "/testPostVar")
public PostParam testPostVar(PostParam param) {
    return param;
}

运行结果:

POST方式总结:

content-type 数据格式 后台接受方式 专用注解
application/x-www-form-urlencoded[默认] json对象 对象 @RequestParam
application/json json字符串 对象 @RequestBody
$.post() 默认application/x-www-form-urlencoded json对象 对象 @RequestParam

Comments ( 0 )

Sign in for post a comment

Java
1
https://gitee.com/java4candy/data-interaction.git
git@gitee.com:java4candy/data-interaction.git
java4candy
data-interaction
data-interaction
master

Help Search