我需要从Java程序调用RESTful Web服务并将其传递给DateTime集合。
所以,我的代码如下:
RestTemplate restTemplate = new RestTemplate();
try {
String scheme = request.getScheme();
String userInfo = request.getRemoteUser();
String host = request.getLocalAddr();
int port = request.getLocalPort();
String path = "/myapp/common/myapi";
MultiValueMap<String, Object> requestParams = new LinkedMultiValueMap<String, Object>();
requestParams.add("aId", objA.getId());
requestParams.add("bIb", objB.getId());
for (DateTime date : dates) {
requestParams.add("dates", date);
}
URI apiUri = new URI(scheme, userInfo, host, port, path, null, null);
result = restTemplate.postForObject(apiUri.toString(), request, BigDecimal.class,
requestParams);
} catch (URISyntaxException e) {
logger.error(e.getMessage());
} catch (DataAccessException e) {
logger.error(e.getMessage());
}
网络服务的签名就像:
@RequestMapping(value = "myapi", method = RequestMethod.POST)
public @ResponseBody BigDecimal myApi(
@RequestParam("dates") final List<DateTime> dates,
@RequestParam("aId") final Integer aId, @RequestParam("bId") final Integer bId) {
[...]
return result;
}
但我收到错误:
org.springframework.http.converter.HttpMessageNotWritableException:无法编写JSON:找不到类java.util.Collections $ 3的序列化程序,并且没有发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))< / p>
我理解问题是集合的序列化,但我不知道如何解决它。
更新 我也尝试过使用Command类,如下所示:
public class CalcolaGiorniResiduiCommand implements Serializable {
private List<DateTime> dates;
//[...]
}
更改了控制器:
@RequestMapping(value = "myapi", method = RequestMethod.POST)
public @ResponseBody BigDecimal myApi(
@RequestParam("command") final MyCommand command) {
最后:
MyCommand command = MyCommand.build(1, 1, dates); //dates is a DateTime Collection
restTemplate.postForObject(apiUri.toString(), request, BigDecimal.class, command);
但我得到同样的错误。
请帮忙吗?
答案 0 :(得分:0)
I think you have to pass a list of dateTime object rather than adding it multiple time. Can you please try with the below snippet.
List<DateTime> dates = new ArrayList<>();
for (DateTime date : dates) {
dates.add(date);
}
requestParams.add("dates", dates);
答案 1 :(得分:0)
从documentation开始,request参数是要POST的对象,因此命令对象必须作为第二个参数而不是对象请求传递。
所以代码应该改为:
a.map(function(element) {
console.log(element);
});
// a
// b
// c
为:
restTemplate.postForObject(apiUri.toString(), request, BigDecimal.class, command);