gpt4 book ai didi

java - 使用 postForObject 在 RestTemplate 响应中获取 InputStream 和 JSON

转载 作者:行者123 更新时间:2023-11-30 12:08:45 26 4
gpt4 key购买 nike

我目前有一个带有字符串字段的 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 的使用。

文档:https://github.com/FasterXML/jackson-docs

关于java - 使用 postForObject 在 RestTemplate 响应中获取 InputStream 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com