如何使用Spring RestTemplate将List或String数组传递给getForObject

问题描述 投票:3回答:3

我正在使用Spring开发一些宁静的服务。我在将字符串数组或大字符串作为参数传递给我的服务控制器时遇到麻烦。我的代码示例如下;

控制器:

@RequestMapping(value="/getLocationInformations/{pointList}", method=RequestMethod.GET)
@ResponseBody
public LocationInfoObject getLocationInformations(@PathVariable("pointList") String pointList)
{
    // code block
}

采样点列表:

String pointList = "37.0433;35.2663,37.0431;35.2663,37.0429;35.2664,37.0428;35.2664,37.0426;35.2665,37.0424;35.2667,37.0422;35.2669,37.042;35.2671,37.0419;35.2673,37.0417;35.2674,37.0415;35.2674,37.0412;35.2672,37.0408;35.267,37.04;35.2667,37.0396;35.2665,37.0391;35.2663,37.0388;35.2662,37.0384;35.266,37.0381;35.2659,37.0379;35.2658,37.0377;35.2657,37.0404;35.2668,37.0377;35.2656,37.0378;35.2652,37.0378;35.2652,37.0381;35.2646,37.0382;35.264,37.0381;35.2635,37.038;35.263,37.0379;35.2627,37.0378;35.2626,37.0376;35.2626,37.0372;35.2627,37.0367;35.2628,37.0363;35.2628,37.036;35.2629,37.0357;35.2629,37.0356;35.2628,37.0356;35.2628,37.0355;35.2626";

Web服务客户端代码:

Map<String, String> vars = new HashMap<String, String>();
vars.put("pointList", pointList);

String apiUrl = "http://api.website.com/service/getLocationInformations/{pointList}";

RestTemplate restTemplate = new RestTemplate();
LocationInfoObject result = restTemplate.getForObject(apiUrl, LocationInfoObject.class, vars);

当我运行客户端应用程序时,它抛出一个HttpClientErrorException: 400 Bad Request,我认为很长的位置信息字符串会导致此问题。那么,我该如何解决这个问题?还是可以将长字符串值作为参数发布到Web服务?

全部谢谢

java spring web-services resttemplate
3个回答
7
投票

列表或其他类型的对象可以使用RestTemplate的postForObject方法进行发布。我的解决方案如下:

控制器:

@RequestMapping(value="/getLocationInformations", method=RequestMethod.POST)
@ResponseBody
public LocationInfoObject getLocationInformations(@RequestBody RequestObject requestObject)
{
    // code block
}

创建要发布到服务的请求对象:

public class RequestObject implements Serializable
{
    public List<Point> pointList    = null;
}

public class Point 
{
    public Float latitude = null;
    public Float longitude = null;
}

创建响应对象以从服务获取值:

public class ResponseObject implements Serializable
{
    public Boolean success                  = false;
    public Integer statusCode               = null;
    public String status                    = null;
    public LocationInfoObject locationInfo  = null;
}

带有请求对象的发布点列表,并从服务获取响应对象:

String apiUrl = "http://api.website.com/service/getLocationInformations";
RequestObject requestObject = new RequestObject();
// create pointList and add to requestObject
requestObject.setPointList(pointList);

RestTemplate restTemplate = new RestTemplate();
ResponseObject response = restTemplate.postForObject(apiUrl, requestObject, ResponseObject.class);

// response.getSuccess(), response.getStatusCode(), response.getStatus(), response.getLocationInfo() can be used

0
投票

首先,您已将地图作为参数传递,但您的控制器希望将它们作为路径变量。您需要做的就是使“ pointlist”值成为URL的一部分(不使用大括号占位符)。例如:-

http://api.website.com/service/getLocationInformations/pointList

接下来,您需要确保已设置消息转换器,以便将LocationInfoObject编组为适当的表示形式(建议JSON)并以相同的方式解组。

对于其余模板:

restTemplate.setMessageConverters(...Google MappingJackson2HttpMessageConverter...);

对于服务器,您只需要将Jackson添加到类路径中(如果您需要多种表示形式,则需要手动配置每个表示形式-Google也会在这里成为您的朋友。


0
投票

问题与GET资源有关,而不与POST有关。因此,我认为“可接受的答案”不是正确的答案。

因此,对于像我这样的其他Google员工,我会添加对我有帮助的内容:

[GET资源可以通过@PathVariable@RequestParam接收字符串列表,甚至通过传递由List<String>分隔的列表,甚至可以将其正确绑定到,

您的API可以是:

@RequestMapping(value="/getLocationInformations/{pointList}", method=RequestMethod.GET)
@ResponseBody
public LocationInfoObject getLocationInformations(@PathVariable("pointList") List<String> pointList) {
    // code block
}

您的电话是:

List<String> listOfPoints = ...;
String points = String.join(",", listOfPoints);

String apiUrl = "http://api.website.com/service/getLocationInformations/{pointList}";
LocationInfoObject result = restTemplate.getForObject(apiUrl, LocationInfoObject.class, points);

请注意,您必须使用,作为分隔符将列表发送到API,否则API无法将其识别为列表。另外,您不能仅将列表直接添加为参数,因为取决于将其混搭的方式,生成的字符串可能不兼容。

© www.soinside.com 2019 - 2024. All rights reserved.