- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前有一个带有字符串字段的 RestTemplate 响应对象来获取响应数据。我想在同一个对象中发送 InputStream。
下面是响应类
@XmlRootElement
public class Test {
private Boolean success;
private String errorMessage;
private String exceptionMessage;
private String confirmation;
private InputStream attachment;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getExceptionMessage() {
return exceptionMessage;
}
public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}
public String getConfirmation() {
return confirmation;
}
public void setConfirmation(String confirmation) {
this.confirmation = confirmation;
}
public InputStream getAttachment() {
return attachment;
}
public void setAttachment(InputStream attachment) {
this.attachment = attachment;
}
}
我正在使用如下的 post 方法。
Test test = restTemplate.postForObject(url,form,Test.class);
传递 inputStream 时出现以下错误。
Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer
请指教。
最佳答案
在使用 JSON 和模型(例如您的示例“测试”中的模型)时,最好的办法是使用一个可以有效地将对象序列化为 JSON 的库。我发现 Jackson 可能是使用大量资源最容易使用的库之一。您也可以使用 Google 的 Gson 库作为替代。
例子
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
服务类
HttpHeaders httpHeaders = < put headers here >
HttpEntity<EdpPartnerBean> entity = new HttpEntity<>(edpPartnerBean, httpHeaders);
// Will automatically use the Jackson serialization
ResponseEntity<Test> response = restTemplate.exchange(url, HttpMethod.POST, entity, Test.class);
测试类
package x;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Test {
private Boolean success;
private String errorMessage;
private String exceptionMessage;
private String confirmation;
private InputStream attachment;
@JsonCreator
public Test(@JsonProperty("success") Boolean success,
@JsonProperty("errorMessage") String errorMessage,
@JsonProperty("exceptionMessage") String exceptionMessage,
@JsonProperty("confirmation") String confirmation,
@JsonProperty("attachment") InputStream attachment) {
this.setSuccess(success);
this.setErrorMessage(errorMessage);
this.setExceptionMessage(exceptionMessage);
this.setConfirmation(confirmation);
this.setAttachment(attachment);
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getExceptionMessage() {
return exceptionMessage;
}
public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}
public String getConfirmation() {
return confirmation;
}
public void setConfirmation(String confirmation) {
this.confirmation = confirmation;
}
public InputStream getAttachment() {
return attachment;
}
public void setAttachment(InputStream attachment) {
this.attachment = attachment;
}
}
注意 JsonCreator 和 JsonProperty 的使用。
关于java - 使用 postForObject 在 RestTemplate 响应中获取 InputStream 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207268/
我在一个类中有两个方法: private static InputStream getSongStream(String ip, String id){ try { U
我创建了一个扩展 InputStream 的新类并且必须 @Override read()。我正在尝试使用方法 read(int b),但是当我使用它时,它会转到方法read() 和我不能使用参数,我
我正在尝试编写一个函数,该函数将接受带有压缩文件数据的 InputStream 并返回另一个带有解压缩数据的 InputStream。 压缩后的文件将只包含一个文件,因此不需要创建目录等... 我尝试
我想知道是否有任何理想的方式可以将多个 InputStream 链接到 Java(或 Scala)中的一个连续 InputStream 中。 我需要它来解析我从 FTP 服务器通过网络加载的平面文件。
我想做的是:打开大文件的 InputStream,按 10MB block 读取它,上传一个 block ,读取下一个 block 。 val chunkCount = Math.ceil(total
我不知道怎么理解: { if (inputStream **!= null**) { inputStream.close(); 来自那个例子: public c
我想知道 InputStream 是否为空,但不使用 read() 方法。有没有办法不读取就知道它是否为空? 最佳答案 不,你不能。 InputStream 设计用于处理远程资源,因此在实际读取它之前
我制作了一个蓝牙输入流监听器,只需询问 if(InputStream.isAvailable > 0) 即可检查是否有传入数据,然后创建一个循环将传入数据存储到 int[] 直到没有更多并且它工作完美
这是我的代码流程,文件内容丢失,我认为可能是 IOUtils.toByteArray() 行有问题,请指导这里实际出了什么问题。 文件内容丢失: InputStream stream = someCl
我从 HTTP 请求的响应开始: InputStream responseInputStream = response.getEntityInputStream() 我需要对该响应进行 gzip 压缩
用户将一个大文件上传到我的网站,我想对该文件进行 gzip 压缩并将其存储在 blob 中。所以我有一个未压缩的 InputStream,而 blob 需要一个 InputStream。我知道如何使用
我调用了一个返回压缩文件的服务。我从响应中将数据作为 InputStream(由 javax.activation.DataHandler.getInputStream(); 提供)提供。 我想做的是
我正在尝试压缩一个 InputStream 并返回一个 InputStream: public InputStream compress (InputStream in){ // Read "in
我最近在 Kotlin 中看到了将 InputStream 的全部内容读入 String 的代码,例如: // input is of type InputStream val baos = Byte
我正在尝试使用以下代码从 IHTTPSession.getInputStream() 读取 InputStream,但它每次都给出 Socket TimeOut Exception。 private
如 How to use Jersey interceptors to get request body 中所述,我正在修改 ContainerRequestFilter 中的 EntityInput
我正在编写一个需要与蓝牙 2.1 设备交换数据的应用程序。我已经做过好几次了,但这次发生了一些奇怪的事情。 Log.d("TAG", "connectToDevice"); if(ma
我只是在犹豫这是好主意还是坏主意: InputStreamReader in = new InputStreamReader(socket.getInputStream()); BufferedRea
我正在开发一个 Android 应用程序,它的 View 包含多个图库。图库的内容(位图)是来自 Internet 的红色。 对于第一个画廊,一切正常,但是当尝试下载第二个画廊的第一张图片时,Bitm
在Dart中,我想读取BMP,所以可能是BIG文件。 我这样做是这样的: var inputStream = imageFile.openInputStream(); inputStream.onDa
我是一名优秀的程序员,十分优秀!