简体   繁体   English

Axios 向springboot后端发布请求

[英]Axios post request to springboot backend

I'm trying to send a formData post request (using axios) to my backend (springboot) but I'm not sure of the proper way to do it.我正在尝试将 formData 发布请求(使用 axios)发送到我的后端(springboot),但我不确定正确的方法。 My plan is to pass the data through the controller to a service that will utilize it.我的计划是通过 controller 将数据传递给将使用它的服务。

Axios call - Axios 调用 -

startStreamLocation() {
  const location = new FormData();
  location.set("accuracy", this.accuracy)
  location.set("lat", this.lat)
  location.set("lng", this.lng)
  location.set("timeStamp", this.timeStamp)

  axios.post("http://localhost:8080/api/v1/location/request-location", location,
      {headers: {'Content-Type': 'application/json'}})
},

Controller - Controller -

@PostMapping(value = "request-location")
public ResponseEntity<?> requestLocation() {

    connectionRequestService.addDataToStream();
    return new ResponseEntity<Authenticator.Success>(HttpStatus.OK);
}

Service -服务 -

   public void addDataToStream() {
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        AmazonKinesis kinesisClient = AmazonKinesisClient.builder()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withRegion(awsRegion)
                .build();

    PutRecordsRequest putRecordsRequest  = new PutRecordsRequest();
    putRecordsRequest.setStreamName("location-stream");
    List <PutRecordsRequestEntry> putRecordsRequestEntryList  = new ArrayList<>();
        PutRecordsRequestEntry putRecordsRequestEntry  = new PutRecordsRequestEntry();
        putRecordsRequestEntry.setData(ByteBuffer.wrap(( INJECT DATA HERE ).getBytes()));
        putRecordsRequestEntry.setPartitionKey(String.format("partitionKey-%d"));
        putRecordsRequestEntryList.add(putRecordsRequestEntry);

    putRecordsRequest.setRecords(putRecordsRequestEntryList);
    PutRecordsResult putRecordsResult  = kinesisClient.putRecords(putRecordsRequest);
    System.out.println("\nData sent successfully... \n" + putRecordsResult);
    try {
        Thread.sleep(1000);
    }
    catch (InterruptedException exception) {
        throw new RuntimeException(exception);
    }
}

Since you want to send form data to the server, you would need to change the Content-Type header in your Axios call to multipart/form-data .由于您想将表单数据发送到服务器,因此您需要将 Axios 调用中的Content-Type header 更改为multipart/form-data This helps the server understand the resource type being sent by the client.这有助于服务器了解客户端发送的资源类型。

On the server end, you'll want to read this form data.在服务器端,您需要读取此表单数据。 I can think of the following two ways to do that我可以想到以下两种方法来做到这一点

  1. Use @RequestParam to read individual form keys.使用@RequestParam读取单个表单键。 For example, if my form data contains a key named Foo , I'd read it on the server end as this例如,如果我的表单数据包含一个名为Foo的键,我会在服务器端读取它,因为
    @PostMapping(value = "/form-data")
    public void readFormData( @RequestParam(value = "Foo") String foo )
  1. Use @RequestBody to map the form data to a MultiValueMap which can be then read from like a normal map.使用@RequestBody到 map 表单数据到MultiValueMap ,然后可以像普通 map 一样读取。 Here's the code snippet for the same这是相同的代码片段
    @PostMapping(value = "/form-data")
    public void readFormData( @RequestBody MultiValueMap<String,String> formData )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM