Java 使用postForObject在RestTemplate响应中获取InputStream和JSON
我目前有一个带有字符串字段的RestTemplate响应对象来获取响应数据。我想在同一个对象中发送InputStream 下面是回复类Java 使用postForObject在RestTemplate响应中获取InputStream和JSON,java,spring,inputstream,resttemplate,Java,Spring,Inputstream,Resttemplate,我目前有一个带有字符串字段的RestTemplate响应对象来获取响应数据。我想在同一个对象中发送InputStream 下面是回复类 @XmlRootElement public class Test { private Boolean success; private String errorMessage; private String exceptionMessage; private String confirmation; private InputStream attachmen
@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可能是最容易使用的库之一,有着丰富的资源。您也可以使用谷歌的Gson库作为替代方案 范例 pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
请注意JsonCreator和JsonProperty的使用
文档:这对postForObject方法不起作用吗?因为我想返回测试是我的Post方法,而不是响应。另外,在测试方法中是否有替代使用构造函数的方法。我不能改变班级的结构。
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;
}
}