简体   繁体   English

Spring Boot:发布到REST API会将单个字符串存储为数组

[英]Spring Boot: Posting to a REST API is storing a single string as an array

I have a Spring Boot app I'm writing that uses Angular for the front end. 我正在编写一个使用Angular前端的Spring Boot应用程序。

One REST API accepts an array of strings as input: 一个REST API接受字符串数组作为输入:

@RequestMapping(value = "/import", method = RequestMethod.POST)
@CrossOrigin
public void importComicFiles(@RequestParam("filenames") String[] filenames)
{
    for (String filename : filenames) { ... }
}

When the front end sends an array of string values using the following: 当前端使用以下命令发送字符串值数组时:

importFiles(filenames: string[]): Observable<Response> {
    const formData: FormData = new FormData();
    for (let index = 0; index < filenames.length; index++) {
      formData.append('filenames', filenames[index]);
    }
    return this.http.post(`${this.apiUrl}/files/import`, formData);
}

then the Java code receives each string as a separate element in the array. 然后Java代码将每个字符串作为数组中的单独元素接收。 Even if those strings have spaces in them. 即使这些字符串中有空格。

HOWEVER, when the importFiles method is called with a single string then the Java code behaves as if the value received is an array spit on a single space at the end of the string. 但是,当使用单个字符串调用importFiles方法时,Java代码的行为就好像接收到的值是字符串末尾的单个空格上的数组。 An example input string is: 输入字符串的示例是:

/Users/mcpierce/Google Drive/comics/DC/2018-01-17/Superman Vol.2016 #39 (March, 2018).cbz / Users / mcpierce / Google Drive / comics / DC / 2018-01-17 / Superman Vol.2016#39(2018年3月).cbz

Java treats this string as if it were two strings: Java将此字符串视为两个字符串:

/Users/mcpierce/Google Drive/comics/DC/2018-01-17/Superman Vol.2016 #39 (March, / Users / mcpierce / Google Drive / comics / DC / 2018-01-17 / Superman Vol.2016#39(3月,

and

2018).cbz 2018)。

If I change the Java code to just treat the filenames parameter as a string (rather than as a List or a String[]) then I get the whole string. 如果将Java代码更改为仅将filenames参数视为字符串(而不是List或String []),则将得到整个字符串。 If it's more than one then each is separated by a common (,). 如果大于一个,则每个分隔符之间用一个公共(,)分隔。

Any ideas? 有任何想法吗?

I would say Spring is operating just fine. 我会说Spring运行得很好。 But the way we use the request params is wrong. 但是我们使用请求参数的方式是错误的。 Spring delimits the request parameters and injects as array. Spring分隔请求参数并作为数组注入。

GET http://example.com?filenames=1,2,3,4 GET http://example.com?filenames=1,2,3,4

Above can be injected into @RequestParam("filenames[]") String[] filenames with each one entry in array. 上面的代码可以注入到@RequestParam("filenames[]") String[] filenames中,数组中的每个条目都可以。

In your case it would be treated as different entries coz of the comma. 在您的情况下,它将被视为逗号的不同条目,例如。 Try posting it as request body object instead as a request param and use to get the array? 尝试将其作为请求主体对象发布,而不是作为请求参数发布并用于获取数组?

您也可以尝试在参数值中添加[]。

@RequestParam("filenames[]") String[] filenames

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

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