body參數 curl post_SpringBoot使用@RequestBody接收参数

使用 curl 传参

出现的问题的描述

使用 curl 命令传输参数,使用 post 请求,后台使用 @RequestBody 接收参数;始终接收不到参数。

最初分析有可能出现的原因:

spring boot jackjson解析的问题

spring boot 的controller除了问题

curl 命令参数出现问题

序列化的其他问题

spring boot 版本出现问题

最终解决问题:是因为控制台编码的问题,在windows下面控制台默认是GBK编码,即使你安装了git-bash,那么控制台编码依然是GBK的,最终在xshell中连接服务器,在连接之后与服务主机进行通信,使用curl命令发送POST请求并且传输参数,发现可以正常传输(在此之前,使用postman进行测试,注意postman使用的时候并不是在param中传输参数,需要在body中传输JSON格式的数据)

Java相关代码

/**

*

*

* @author 冰羽

* @version 1.0.0

*/

@Data

@AllArgsConstructor

@NoArgsConstructor

public class User implements Serializable {

private String id;

private String username;

private String password;

}

注意:需要序列化

import com.example.demo.core.ApiResult;

import com.example.demo.entity.User;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

/**

*

*

* @author 冰羽

* @version 1.0.0

*/

@Controller

@RequestMapping(value = "/user")

@Slf4j

public class UserController {

@PostMapping

@ResponseBody

public ApiResult> save(@RequestBody User user) {

log.info("save user: {}", user);

return ApiResult.ofSuccess();

}

}

curl 发送请求 (在控制台编码为UTF-8的xshell)

curl -H "Content-Type:application/json;charset=UTF-8" -XPOST http://192.168.31.97:60315/user -d '{

"username": "冰羽",

"password": "123456"

}'

Idea控制台输出

2020-03-22 21:08:08.515 DEBUG 14856 --- [io-60315-exec-7] o.s.web.servlet.DispatcherServlet : POST "/user", parameters={}

2020-03-22 21:08:08.516 DEBUG 14856 --- [io-60315-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public com.example.demo.core.ApiResult> com.example.demo.controller.UserController.save(com.example.demo.entity.User)

2020-03-22 21:08:08.517 DEBUG 14856 --- [io-60315-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [User(id=null, username=冰羽, password=123456)]

2020-03-22 21:08:08.518 INFO 14856 --- [io-60315-exec-7] c.e.demo.controller.UserController : save user: User(id=null, username=冰羽, password=123456)

2020-03-22 21:08:08.519 DEBUG 14856 --- [io-60315-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json]

2020-03-22 21:08:08.519 DEBUG 14856 --- [io-60315-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Writing [com.example.demo.core.ApiResult@528d9498]

2020-03-22 21:08:08.520 DEBUG 14856 --- [io-60315-exec-7] o.s.web.servlet.DispatcherServlet : Completed 200 OK

curl 发送请求 (在控制台编码为GBK的Windows gib-bash 发送中文)

curl -H "Content-Type:application/json;charset=UTF-8" -XPOST http://192.168.31.97:60315/user -d '{

"username": "冰羽",

"password": "123456"

}'

Idea控制台输出

2020-03-22 21:10:39.470 DEBUG 14856 --- [o-60315-exec-10] o.s.web.servlet.DispatcherServlet : POST "/user", parameters={}

2020-03-22 21:10:39.470 DEBUG 14856 --- [o-60315-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public com.example.demo.core.ApiResult> com.example.demo.controller.UserController.save(com.example.demo.entity.User)

2020-03-22 21:10:39.471 DEBUG 14856 --- [o-60315-exec-10] .w.s.m.m.a.ServletInvocableHandlerMethod : Could not resolve parameter [0] in public com.example.demo.core.ApiResult> com.example.demo.controller.UserController.save(com.example.demo.entity.User): JSON parse error: Invalid UTF-8 start byte 0xb1; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xb1

at [Source: (PushbackInputStream); line: 2, column: 17] (through reference chain: com.example.demo.entity.User["username"])

2020-03-22 21:10:39.472 WARN 14856 --- [o-60315-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid UTF-8 start byte 0xb1; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xb1

at [Source: (PushbackInputStream); line: 2, column: 17] (through reference chain: com.example.demo.entity.User["username"])]

2020-03-22 21:10:39.472 DEBUG 14856 --- [o-60315-exec-10] o.s.web.servlet.DispatcherServlet : Completed 400 BAD_REQUEST

2020-03-22 21:10:39.473 DEBUG 14856 --- [o-60315-exec-10] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={}

2020-03-22 21:10:39.473 DEBUG 14856 --- [o-60315-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String com.example.demo.controller.ErrorController.getErrorPath()

2020-03-22 21:10:39.474 DEBUG 14856 --- [o-60315-exec-10] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 400

Git-bash控制台输出并接收到返回

$ curl -H "Content-Type:application/json;charset=UTF-8" -XPOST http://192.168.31.97:60315/user -d '{

> "username": "冰羽",

> "password": "123456"

> }'

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

null

There was an unexpected error (type=null, status=null).

curl 发送请求 (在控制台编码为GBK的Windows gib-bash 发送非中文)

curl -H "Content-Type:application/json;charset=UTF-8" -XPOST http://192.168.31.97:60315/user -d '{

"username": "IOX",

"password": "123456"

}'

Idea控制台输出

2020-03-22 21:12:22.520 DEBUG 14856 --- [io-60315-exec-5] o.s.web.servlet.DispatcherServlet : POST "/user", parameters={}

2020-03-22 21:12:22.520 DEBUG 14856 --- [io-60315-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public com.example.demo.core.ApiResult> com.example.demo.controller.UserController.save(com.example.demo.entity.User)

2020-03-22 21:12:22.521 DEBUG 14856 --- [io-60315-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [User(id=null, username=IOX, password=123456)]

2020-03-22 21:12:22.521 INFO 14856 --- [io-60315-exec-5] c.e.demo.controller.UserController : save user: User(id=null, username=IOX, password=123456)

2020-03-22 21:12:22.521 DEBUG 14856 --- [io-60315-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json]

2020-03-22 21:12:22.521 DEBUG 14856 --- [io-60315-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Writing [com.example.demo.core.ApiResult@4dfe490d]

2020-03-22 21:12:22.522 DEBUG 14856 --- [io-60315-exec-5] o.s.web.servlet.DispatcherServlet : Completed 200 OK

Git-bash控制台输出并接收到返回

$ curl -H "Content-Type:application/json;charset=UTF-8" -XPOST http://192.168.31.97:60315/user -d '{

> "username": "IOX",

> "password": "123456"

> }'

{"code":200,"message":"操作成功","data":null}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值