![](/img/trans.png)
[英]Map JSON leaf to an object via RestTemplate postForObject
[英]Get InputStream and JSON in RestTemplate Response with postForObject
我目前有一个带有String字段的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;
}
}
我正在使用以下发布方法。
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>
Service.class
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);
的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的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.