Получите InputStream и JSON в ответе RestTemplate с помощью postForObject - PullRequest
0 голосов
/ 16 января 2019

В настоящее время у меня есть объект ответа RestTemplate с полями String для получения данных ответа. Я хочу отправить 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

Пожалуйста, сообщите.

1 Ответ

0 голосов
/ 16 января 2019

Когда дело доходит до работы с JSON и моделями, подобными моделям в вашем примере «Test», лучше всего использовать библиотеку, которая эффективно сериализует объекты в JSON. Я считаю, что Джексон, вероятно, одна из самых простых библиотек для использования с большим количеством ресурсов. Вы также можете использовать библиотеки 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

...