Java 如何使用 Spring RestTemplate 将 List 或 String 数组传递给 getForObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22507129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass List or String array to getForObject with Spring RestTemplate
提问by vtokmak
I am developing some restful services with Spring. I have trouble with passing/getting string array or large string as parameters to my service controller. My code examples are like below;
我正在用 Spring 开发一些宁静的服务。我无法将字符串数组或大字符串作为参数传递/获取到我的服务控制器。我的代码示例如下;
Controller:
控制器:
@RequestMapping(value="/getLocationInformations/{pointList}", method=RequestMethod.GET)
@ResponseBody
public LocationInfoObject getLocationInformations(@PathVariable("pointList") String pointList)
{
// code block
}
Sample point list:
样本点列表:
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 service client code:
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);
When I run client side application, it throws a HttpClientErrorException: 400 Bad Request
, I think long location information string causes to this problem. So, how can I solve this issue? Or is it possible posting long string value as parameter to web service?
当我运行客户端应用程序时,它抛出一个HttpClientErrorException: 400 Bad Request
,我认为长位置信息字符串会导致此问题。那么,我该如何解决这个问题呢?或者是否可以将长字符串值作为参数发布到 Web 服务?
Thx all
谢谢所有
采纳答案by vtokmak
List or other type of objects can post with RestTemplate's postForObject method. My solution is like below:
列表或其他类型的对象可以使用 RestTemplate 的 postForObject 方法发布。我的解决方案如下:
controller:
控制器:
@RequestMapping(value="/getLocationInformations", method=RequestMethod.POST)
@ResponseBody
public LocationInfoObject getLocationInformations(@RequestBody RequestObject requestObject)
{
// code block
}
Create a request object for posting to service:
创建一个用于发布到服务的请求对象:
public class RequestObject implements Serializable
{
public List<Point> pointList = null;
}
public class Point
{
public Float latitude = null;
public Float longitude = null;
}
Create a response object to get values from service:
创建一个响应对象以从服务中获取值:
public class ResponseObject implements Serializable
{
public Boolean success = false;
public Integer statusCode = null;
public String status = null;
public LocationInfoObject locationInfo = null;
}
Post point list with request object and get response object from service:
带有请求对象的发布点列表并从服务中获取响应对象:
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
回答by user1016765
Firstly you've passed a map as parameters but your controller expects these as a path variable. All you need to do is make the 'pointlist' value part of the URL (without the curly bracket placeholders). e.g.:-
首先,您已将地图作为参数传递,但您的控制器希望将这些作为路径变量。您需要做的就是将“pointlist”值作为 URL 的一部分(没有大括号占位符)。例如:-
http://api.website.com/service/getLocationInformations/pointList
Next you need to ensure you have message converters set up so that your LocationInfoObject is marshalled into an appropriate representation (suggest JSON) and unmarshalled the same way.
接下来,您需要确保设置了消息转换器,以便将 LocationInfoObject 编组为适当的表示(建议 JSON)并以相同的方式解组。
For the Rest template:
对于 Rest 模板:
restTemplate.setMessageConverters(...Google MappingHymanson2HttpMessageConverter...);
For the server you just need to add Hymanson to the classpath (if you want multiple representations you'd need to configure each one manually - Google will be your friend here aswell.
对于服务器,您只需要将 Hymanson 添加到类路径中(如果您想要多个表示,则需要手动配置每个表示 - Google 也将成为您的朋友。