简体   繁体   English

如何使用ARC扩展或ANGULAR5发送POST请求以测试api Rest?

[英]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之外什么都不做,我会在它工作后改变它..

For ARC extention : 对于ARC扩展: 在此处输入图片说明

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.

相关问题 如何使用Rest-Assured请求POST API发送令牌和主体值? - How to request a POST API send token and body values using Rest-Assured? 如何使用java在REST api中通过post请求将多个json文档发送到marklogic数据库? - How to send multiple json documents into marklogic database through post request in REST api using java? 发送请求到Spring Rest API - Send post request to spring rest api "如何使用 REST Assured 在正文中将请求作为 Json 数组发送以进行 Post" - How to send Request as Json Array in body for Post using REST Assured 如何使用 ClientBuilder 为 Rest Post Api MultiPart 编写集成测试 - How to write Integration Test for Rest Post Api MultiPart using ClientBuilder 如何在 POST 请求中发送数组 rest 放心 - how to send an array in POST request rest assured 如何使用Spring Boot向API发送特定的POST请求 - How to send a specific POST request to an API using Spring Boot 如何在POST方法的REST API中发送日期 - How to send Date in REST API in POST method 如何使用邮递员休息客户端将邮递请求发送到以下邮递方法 - How to send post request to the below post method using postman rest client Java:发送POST HTTP请求以将文件上传到MediaFire REST API - Java: send POST HTTP request to upload a file to MediaFire REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM