簡體   English   中英

使用postForObject在RestTemplate響應中獲取InputStream和JSON

[英]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的使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM