1、函数式:
//1、若是form-data形式提交表单内容,即Content-Type: application/x-www-form-urlencoded;可以通过ServerWebExchange 获取
$.ajax({
type: 'POST',
url: "/post/findform",
data: {
name: "aa",
sex:"n"
},
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
// route:
@Configuration
public class PostRouter {
@Bean
public RouterFunction postroute(PostHandler handler) {
return RouterFunctions.route(RequestPredicates.POST("/post/findform")
.and(RequestPredicates.accept(MediaType.APPLICATION_FORM_URLENCODED)),handler::findform);
}
}
// handle:
public Mono findform(ServerRequest request){
// 获取form data,在方法中添加ServerWebExchange对象,使用方法getFormData
ServerWebExchange exchange = request.exchange();
Mono> formData =exchange.getFormData();
return formData.flatMap(map -> {
System.out.println(map.get("name").get(0));
System.out.println(map.get("sex").get(0));
return ServerResponse.ok().contentType(MediaType.APPLICATION_STREAM_JSON)
.body(BodyInserters.fromObject(map));
});
}
// 2、若是application/json的形式提交表单
$.ajax({
type: 'POST',
url: "/post/findjson",
data: JSON.stringify({
"name": "aa"
}),
contentType:"application/json",
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
// route:
@Configuration
public class PostRouter {
@Bean
public RouterFunction postroute(PostHandler handler) {
return RouterFunctions.route(RequestPredicates.POST("/post/findjson")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),handler::find)
}
}
// handle:直接通过map映射获取
public Mono find(ServerRequest serverRequest){
Mono body = serverRequest.bodyToMono(Map.class);
return body.flatMap(map -> {
String name = (String) map.get("name");
System.out.println(name);
return ServerResponse.ok().contentType(MediaType.APPLICATION_STREAM_JSON)
.body(BodyInserters.fromObject(name));
});
}
2、注解式
若是get请求,可通过@RequestParam或直接获取
// get形式提交,下面两种形式都可以直接获取到请求参数
@GetMapping("/save")
private Mono save(final User user){
System.out.println(user.getName());
return Mono.justOrEmpty(userService.save(user));
}
@GetMapping("/save1")
private Mono save(@RequestParam final String name,@RequestParam final String sex){
System.out.println(name);
return Mono.justOrEmpty(userService.save(new User(name,sex)));
}
若是Post请求,当请格式分别为json或表单格式,json使用@RequestBody获取,form格式通过实体映射或使用ServerWebExchange获取
$.ajax({
type: 'POST',
url: "/postController/json",
data: JSON.stringify({
"name": "aa",
"sex":"m"
}),
contentType:"application/json",
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
$.ajax({
type: 'POST',
url: "/postController/jsonS",
data: JSON.stringify({
"name": "aa",
"sex":"m"
}),
contentType:"application/json",
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
$.ajax({
type: 'POST',
url: "/postController/form",
data: {
name: "bb",
sex:"m"
},
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
$.ajax({
type: 'POST',
url: "/postController/formS",
data: {
name: "bb",
sex:"m"
},
dataType: "json",
success: function (data) {
console.log(data);
},error:function () {
console.log("error");
}
});
// controller
@RestController
@RequestMapping("/postController")
public class PostController {
/**
* 通过@RequestBody获取提交的application/json数据
* @param
* @return
*/
@PostMapping(value = "/json",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Mono json(@RequestBody String user) {
System.out.println("json:"+user);
return Mono.just(user);
}
/**
* 通过实体映射获取post的请求体 application/json数据
* @param user
* @return
*/
@PostMapping(value = "/jsonS",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Mono jsonS(@RequestBody User user) {
System.out.println("jsonS:"+ user);
return Mono.just(user.toString());
}
/**
* 通过实体映射获取表单数据
* request:Content-Type: application/x-www-form-urlencoded
* @param user
* @return
*/
@PostMapping(value = "/form", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Mono save(User user) {
System.out.println("form:"+user);
return Mono.justOrEmpty(user);
}
/**
* 通过ServerWebExchange获取表单数据
* request:Content-Type: application/x-www-form-urlencoded
* @param exchange
* @return
*/
@PostMapping(value = "/formS", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Mono save(ServerWebExchange exchange) {
Mono> formData =exchange.getFormData();
return formData.flatMap(map -> {
System.out.println("formS:"+map);
return Mono.justOrEmpty(map.toString());
});
}
}