使用 curl
命令传输参数,使用 post
请求,后台使用 @RequestBody
接收参数;始终接收不到参数。
最初分析有可能出现的原因:
最终解决问题:是因为控制台编码的问题,在windows下面控制台默认是GBK编码,即使你安装了git-bash,那么控制台编码依然是GBK的,最终在xshell中连接服务器,在连接之后与服务主机进行通信,使用curl命令发送POST请求并且传输参数,发现可以正常传输(在此之前,使用postman进行测试,注意postman使用的时候并不是在param中传输参数,需要在body中传输JSON格式的数据)
/**
*
*
* @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 -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 -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.
nullThere was an unexpected error (type=null, status=null).
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}