我如何通过URL将list <string>传递给服务器

时间:2019-04-26 05:10:02

标签: java arrays list spring-boot

我正在尝试在春季启动时将字符串列表从一台服务器传递到另一台服务器。 我如何在另一台服务器上获取该列表?

我尝试过的代码-

 foreach (InlineShape shape in doc.InlineShapes)
 {
       if (shape.OLEFormat != null && shape.OLEFormat.ClassType == "CONTROL Forms.TextBox.1")
       {
              Console.WriteLine("Data :" + shape.OLEFormat.Object.Value);
       }
 }

在服务器端,我试图变得像-

public void addNewMostPopular(List<String> totalList){  
    try {
        HttpHeaders httpHeaders = getHttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(null, httpHeaders);

        ResponseEntity responseEntity = restTemplate.exchange(
                BASE_URL + "addMostPopular/"+new ArrayList<>(totalList), HttpMethod.POST, httpEntity,TrendingCategoryDTO.class);
    }

3 个答案:

答案 0 :(得分:1)

URL中的过去的长对象是错误的做法,那是因为spring URL解释器具有最大长度,因此,如果在某些情况下传递的值超过2048或4096个字符,则您的请求将返回响应400错误的请求,并且将不会执行您的Spring服务器上的任何代码。

此声明之后,是否可以通过列表?当然是!但是我们需要像这样使用@RequestBody

@PostMapping("/addMostPopular")
public void addMostPopularProduct(@RequestBody List<String> totalList) {
    // Your function
}

现在,我们需要在请求正文中添加要传递给该请求的列表。

答案 1 :(得分:0)

如果您希望在url中传递值列表,则可以将它们作为url参数传递。

您必须创建一个类似于以下内容的链接:

http://youserver/youraction?param=first&param=second&param=third

http://youserver/youraction?param=first,second,third

您春季的控制器必须类似

@Controller 
public class MyController {

    @GetMapping("/youraction")
    public String yourAction(@RequestParam("param") List<String> params) {
       // Here params is tre list with the values first, second, third
    }
}

此操作能够处理我之前编写的两种请求。

答案 2 :(得分:0)

有许多方法可以在服务器之间传递信息。 简单的方法是根据您的请求方法get或post将参数放置到适当的位置来发起http请求:reuqest标头或请求正文。您可以像@Davide Lorenzo MARINO一样。 或使用消息队列,例如ActiveMq。 如果是同一注册中心,则也可以使用@feign来解决它。