我无法通过HttpEntity调用Spring RestTemplate进行POST请求。 我对RestTemplate的调用在Postman中给出了Base64字符串,但是使用我的java实现,它给出了以下错误:
java.lang.IllegalArgumentException: Illegal base64 character 5b
at java.util.Base64$Decoder.decode0(Base64.java:714)
at java.util.Base64$Decoder.decode(Base64.java:526)
at java.util.Base64$Decoder.decode(Base64.java:549)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
我的实现是:
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
final HttpEntity<String> request = new HttpEntity<String>(searchRequestInput, headers);
final ResponseEntity<String> postForEntity = restTemplate
.postForEntity(baseURL, request, String.class);
String response = postForEntity.getBody();
我尝试了以下解决方案,但是没有用 here
答案 0 :(得分:0)
您可以使用HttpMessageConverter请求对HttpEntity的restTemplate调用。 可以从HTTP请求和响应中读取和写入字符串。
来自doc:默认情况下,此转换器支持所有文本媒体类型(text / *),并使用Content / Type为text / plain的内容进行写入。
您可以通过实现StringHttpMessageConverter来尝试以下操作:
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
final HttpEntity<String> request = new HttpEntity<String>(searchRequestInput, headers);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
然后使用您的请求调用restTemplate。
答案 1 :(得分:0)
尝试以下实用方法:
public static <T> ResponseEntity<T> makeRestRequest(Object entity, String restUrl, HttpMethod method, Class<T> entityClass) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = makeHttpEntity(entity);
ResponseEntity<T> response = null;
try {
response = restTemplate.exchange(restUrl, method, httpEntity, entityClass);
} catch (HttpClientErrorException e) {
e.printStackTrace();
return new ResponseEntity<>(e.getStatusCode());
}
return response;
}
public static <T> HttpEntity makeHttpEntity(T entity) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<T> httpEntity = new HttpEntity<>(entity, headers);
return httpEntity;
}
这两种方法中的
- 实体:您的输入对象
- restUrl:您的网址
- HttpMethod:POST / GET
- entityClass:服务器的预期输出对象
答案 2 :(得分:0)
请尝试以下代码,可能对您有帮助:
override func collectionView(_ collectionView: QMChatCollectionView!, layoutModelAt indexPath: IndexPath!) -> QMChatCellLayoutModel {
var layoutModel: QMChatCellLayoutModel = super.collectionView(collectionView, layoutModelAt: indexPath)
layoutModel.avatarSize = CGSize(width: 0, height: 0)
layoutModel.topLabelHeight = 0.0
layoutModel.spaceBetweenTextViewAndBottomLabel = 5
layoutModel.maxWidthMarginSpace = 20.0
guard let item = self.chatDataSource.message(for: indexPath) else {
return layoutModel
}
let viewClass: AnyClass = self.viewClass(forItem: item)! as AnyClass
if viewClass === QMChatIncomingCell.self || viewClass === QMChatAttachmentIncomingCell.self //|| viewClass === QMVideoIncomingCell.self
{
if self.dialog.type != QBChatDialogType.private {
let topAttributedString = self.topLabelAttributedString(forItem: item)
let size = TTTAttributedLabel.sizeThatFitsAttributedString(topAttributedString, withConstraints: CGSize(width: collectionView.frame.width - kMessageContainerWidthPadding, height: CGFloat.greatestFiniteMagnitude), limitedToNumberOfLines:1)
layoutModel.topLabelHeight = size.height
//layoutModel.topLabelHeight = 0
}
layoutModel.spaceBetweenTopLabelAndTextView = 5
}
var size = CGSize.zero
if self.detailedCells.contains(item.id!) {
let bottomAttributedString = self.bottomLabelAttributedString(forItem: item)
size = TTTAttributedLabel.sizeThatFitsAttributedString(bottomAttributedString, withConstraints: CGSize(width: collectionView.frame.width - kMessageContainerWidthPadding, height: CGFloat.greatestFiniteMagnitude), limitedToNumberOfLines:0)
}
layoutModel.bottomLabelHeight = floor(size.height)
return layoutModel
}