[英]How to send POST request to test the api Rest using ARC extention or ANGULAR5?
I'm trying to test the api rest in spring boot , so i'm using the ARC extention to send a POST request but i guess i'm doing it wrong . 我正在尝试在春季启动时测试api rest,因此我正在使用ARC扩展发送POST请求,但我想我做错了。
After authentication i get the token that i use to test the api rest . 身份验证后,我得到了用于测试api rest的令牌。
so here is the method in spring boot : 所以这是spring boot中的方法:
@RequestMapping(value="/saveProjectToClient",method=RequestMethod.POST)
public boolean saveProjectToClient(@RequestBody DTO dto){
System.out.println("Person id : "+dto.getIdPerson());
System.out.println("Project id : "+dto.getIdProject());
return true;
}
this method is not doing anything but showing the id , i'll change that after it works .. 这个方法除了显示id之外什么都不做,我会在它工作后改变它..
To send the parameter in body : 要在body中发送参数:
As you can see i get an error : 如您所见,我得到一个错误:
{
"timestamp": 1526330389396,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not deserialize instance of long out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of long out of START_OBJECT token at [Source: java.io.PushbackInputStream@1134a39; line: 1, column: 1]",
"path": "/saveProjectToClient"
}
What's the problem ? 有什么问题 ? i'm using the post request in incorrect way ? 我使用邮寄要求的方式有误吗? or the method in spring boot is not well written ? 还是Spring Boot中的方法写得不好?
EDIT 编辑
Angular5 Service using HttpClient : 使用HttpClient的Angular5服务:
addProjToClient(idPerson:number,idProject:number){
if(this.authService.getToken()==null) {
this.authService.loadToken();
}
return this.http.post(this.host+"/saveProjectToClient", {
idPerson,
idProject,
}, {headers:new HttpHeaders({'Authorization':this.authService.getToken()})})
}
Problem : Now when i try to call this method by the angular5 service , i don't get any result and even more no errors , the method in spring boot is unreachable . 问题 :现在,当我尝试通过angular5服务调用此方法时,我没有得到任何结果,甚至没有任何错误,因此无法使用spring boot中的方法。
The problem is clearly stated in the exception message: 在异常消息中明确指出了该问题:
Can not deserialize instance of long out of START_OBJECT
Jackson cannot deserialize a long when it finds a starting {
character in your JSON request body. 当在JSON请求正文中找到起始{
字符时,Jackson不能反序列化很长时间。 Wrapping the id in a model like the following should suffice: 将id包装在如下所示的模型中就足够了:
public class MyModel {
private Long id; // use primitive type if, as I would think, id cannot be null
public void setId( Long id ) {
this.id = id;
}
public Long getId() {
return id;
}
}
You then obviously have to tell Spring to use the custom model as the @RequestBody
: 然后,您显然必须告诉Spring将自定义模型用作@RequestBody
:
@RequestMapping( value = "/saveProjectToClient", method = RequestMethod.POST )
public boolean saveProjectToClient( @RequestBody MyModel model ) {
System.out.println( "ID: " + String.valueOf( model.getId() );
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.