使用postForObject

时间:2019-01-15 21:41:41

标签: java spring inputstream resttemplate

我目前有一个带有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

请告知。

1 个答案:

答案 0 :(得分:0)

在处理JSON和模型(如示例“测试”中的模型)时,最好的选择是使用一个可有效地将对象序列化为JSON的库。我发现Jackson可能是最容易使用的具有大量资源的库之一。您也可以使用Google的Gson库作为替代。

示例

pom.xml

    <nav id="site-navigation" class="main-navigation" role="navigation" aria-label="Main menu" data-userway-font-size="14">
         <div class="menu-main-menu-container" data-userway-font-size="14"><ul id="primary-menu" class="menu" data-userway-font-size="14"><li id="menu-item-24" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-24" data-userway-font-size="14"><a href="http://stg.alamedactc.org/about-us/" data-userway-font-size="16" class="userway-s1-selected">About Us  <i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-323" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-323" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/commission/" data-userway-font-size="19" class="">Commission</a></li>
     <li id="menu-item-408" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-408" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/committees/" data-userway-font-size="19">Committees</a></li>
     <li id="menu-item-405" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-405" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/contact-us/" data-userway-font-size="19">Contact Us</a></li>
     <li id="menu-item-324" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-324" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/faqs/" data-userway-font-size="19">FAQs</a></li>
     <li id="menu-item-325" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-325" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/financials/" data-userway-font-size="19">Financials</a></li>
     <li id="menu-item-401" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-401" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/legislative-program/" data-userway-font-size="19">Legislative Program</a></li>
     <li id="menu-item-326" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-326" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/partners/" data-userway-font-size="19">Partners</a></li>
     <li id="menu-item-402" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-402" data-userway-font-size="16"><a href="http://stg.alamedactc.org/about-us/staff/" data-userway-font-size="19">Staff</a></li>
    </ul>
    </li>
    <li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-29" data-userway-font-size="14"><a href="http://stg.alamedactc.org/planning/" data-userway-font-size="16">Planning  <i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-327" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-327" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/complete-streets-in-alameda-county/" data-userway-font-size="19">Complete Streets</a></li>
     <li id="menu-item-328" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-328" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/congestion-management/" data-userway-font-size="19">Congestion Management</a></li>
     <li id="menu-item-411" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-411" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/countywide-bicycle-ped-plan/" data-userway-font-size="19">Active Transportation</a></li>
     <li id="menu-item-329" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-329" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/countywide-multi-arterial-plan/" data-userway-font-size="19">Countywide Multimodal Arterial Plan</a></li>
     <li id="menu-item-330" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-330" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/countywide-transit-plan/" data-userway-font-size="19">Countywide Transit Plan</a></li>
     <li id="menu-item-331" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-331" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/countywidetransportationplan/" data-userway-font-size="19">Countywide Transportation Plan</a></li>
     <li id="menu-item-332" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-332" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/goodsmovement/" data-userway-font-size="19">Goods Movement</a></li>
     <li id="menu-item-333" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-333" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/priority-development-area-investment-and-growth-strategy/" data-userway-font-size="19">Priority Development Area Investment and Growth Strategy</a></li>
     <li id="menu-item-410" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-410" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/sustainable-communities-technical-assistance/" data-userway-font-size="19">Sustainable Communities Technical Assistance</a></li>
     <li id="menu-item-409" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-409" data-userway-font-size="16"><a href="http://stg.alamedactc.org/planning/technology/" data-userway-font-size="19">Technology</a></li>
    </ul>
    </li>
    <li id="menu-item-25" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-25" data-userway-font-size="14"><a href="http://stg.alamedactc.org/funding/" data-userway-font-size="16">Funding  <i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-5689" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-5689" data-userway-font-size="16"><a href="http://stg.alamedactc.org/funding/fund-sources/" data-userway-font-size="19">Fund Sources</a></li>
     <li id="menu-item-5688" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-5688" data-userway-font-size="16"><a href="http://stg.alamedactc.org/funding/funding-opportunities/" data-userway-font-size="19">Funding Opportunities</a></li>
     <li id="menu-item-5687" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-5687" data-userway-font-size="16"><a href="http://stg.alamedactc.org/funding/reporting-and-grant-forms/" data-userway-font-size="19">Reporting and Grant Forms</a></li>
    </ul>
    </li>
    <li id="menu-item-48" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-48" data-userway-font-size="14"><a href="http://stg.alamedactc.org/programs-projects/" data-userway-font-size="16">Projects and Programs  <i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-7699" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-7699" data-userway-font-size="16"><a href="/programs-projects/" data-userway-font-size="19">Projects</a>
     <ul class="sub-menu" data-userway-font-size="16">
       <li id="menu-item-338" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-338" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/bicycle-and-pedestrian/" data-userway-font-size="23">Bicycle and Pedestrian</a></li>
       <li id="menu-item-7082" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-7082" data-userway-font-size="19"><a href="/programs-projects/expresslanes/" data-userway-font-size="23">Express Lanes</a></li>
       <li id="menu-item-342" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-342" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/highway-improvement/" data-userway-font-size="23">Highway Improvements</a></li>
       <li id="menu-item-343" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-343" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/multimodal-arterial-roads/" data-userway-font-size="23">Multimodal Arterials and Roads</a></li>
       <li id="menu-item-344" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-344" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/port-of-oakland/" data-userway-font-size="23">Port of Oakland Infrastructure</a></li>
       <li id="menu-item-413" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-413" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/transit/" data-userway-font-size="23">Transit</a></li>
     </ul>
    </li>
     <li id="menu-item-7700" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-7700" data-userway-font-size="16"><a href="/programs-projects/" data-userway-font-size="19">Programs</a>
     <ul class="sub-menu" data-userway-font-size="16">
       <li id="menu-item-4550" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-4550" data-userway-font-size="19"><a href="/programs-projects/bicycle-and-pedestrian/" data-userway-font-size="23">Bicycle and Pedestrian</a></li>
       <li id="menu-item-339" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-339" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/commute-options-and-benefits/" data-userway-font-size="23">Commute Options and Benefits</a></li>
       <li id="menu-item-2714" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2714" data-userway-font-size="19"><a href="http://stg.alamedactc.org/safe-routes-to-schools/" data-userway-font-size="23">Safe Routes to Schools</a></li>
       <li id="menu-item-345" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-345" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/senior-disabled-transport/" data-userway-font-size="23">Senior and Disabled Transportation</a></li>
       <li id="menu-item-346" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-346" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/studentpass/" data-userway-font-size="23">Student Transit Pass Pilot</a></li>
       <li id="menu-item-414" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-414" data-userway-font-size="19"><a href="http://stg.alamedactc.org/programs-projects/transit-oriented-development/" data-userway-font-size="23">Transit Oriented Development</a></li>
     </ul>
    </li>
    </ul>
    </li>
    <li id="menu-item-26" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-26" data-userway-font-size="14"><a href="http://stg.alamedactc.org/get-involved/" data-userway-font-size="16">Get Involved  <i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-418" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-418" data-userway-font-size="16"><a href="http://stg.alamedactc.org/get-involved/careers-jobs/" data-userway-font-size="19">Career Opportunities</a></li>
     <li id="menu-item-419" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-419" data-userway-font-size="16"><a href="http://stg.alamedactc.org/get-involved/contracting-doing-business/" data-userway-font-size="19">Contracting  Opportunities</a></li>
     <li id="menu-item-3483" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3483" data-userway-font-size="16"><a href="/get-involved/upcoming-meetings/" data-userway-font-size="19">Meetings</a></li>
     <li id="menu-item-421" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-421" data-userway-font-size="16"><a href="http://stg.alamedactc.org/get-involved/sign-up/" data-userway-font-size="19">Sign Up and Stay Informed</a></li>
    </ul>
    </li>
    <li id="menu-item-47" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-47" data-userway-font-size="14"><a href="http://stg.alamedactc.org/news-publications/" data-userway-font-size="16">News&nbsp;and&nbsp;Publications&nbsp;<i class="arrow down" data-userway-font-size="16"></i></a>
    <ul class="sub-menu" data-userway-font-size="14">
     <li id="menu-item-423" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-423" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news-publications/e-newsletter/" data-userway-font-size="19">E-Newsletter</a></li>
     <li id="menu-item-424" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-424" data-userway-font-size="16"><a href="http://stg.alamedactc.org/expenditure-plans/" data-userway-font-size="19">Expenditure Plans</a></li>
     <li id="menu-item-425" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-425" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news-publications/fact-sheets/" data-userway-font-size="19">Fact Sheets</a></li>
     <li id="menu-item-2757" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2757" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news/" data-userway-font-size="19">News</a></li>
     <li id="menu-item-2476" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2476" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news-publications/press-releases/" data-userway-font-size="19">Press Releases</a></li>
     <li id="menu-item-426" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-426" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news-publications/reports/" data-userway-font-size="19">Reports</a></li>
     <li id="menu-item-427" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-427" data-userway-font-size="16"><a href="http://stg.alamedactc.org/news-publications/signage/" data-userway-font-size="19">Signage</a></li>
    </ul>
    </li>
    </ul></div>     </nav>

Service.class

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

Test.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);

注意使用JsonCreator和JsonProperty。

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