欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 中HttpClient傳輸xml字符串實例詳解

 更新時間:2017年04月19日 08:38:55   投稿:lqh  
這篇文章主要介紹了java 中HttpClient傳輸xml字符串實例詳解的相關(guān)資料,需要的朋友可以參考下

java 中HttpClient傳輸xml字符串實例詳解

介紹:我現(xiàn)在有一個對象page,需要將page對象轉(zhuǎn)換為xml格式并以binary方式傳輸?shù)椒?wù)端

其中涉及到的技術(shù)點有:

1、對象轉(zhuǎn)xml流
2、輸出流轉(zhuǎn)輸入流
3、httpClient發(fā)送二進制流數(shù)據(jù)

POM文件依賴配置

<dependencies> 
  <dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>3.8.1</version> 
    <scope>test</scope> 
  </dependency> 
  <dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpclient</artifactId> 
    <version>4.5.2</version> 
  </dependency> 
   
  <dependency> 
   <groupId>org.apache.httpcomponents</groupId> 
   <artifactId>httpmime</artifactId> 
   <version>4.5.2</version> 
  </dependency> 
  <dependency> 
    <groupId>commons-lang</groupId> 
    <artifactId>commons-lang</artifactId> 
    <version>2.4</version> 
  </dependency> 
  <dependency> 
    <groupId>com.google.code.gson</groupId> 
    <artifactId>gson</artifactId> 
    <version>2.2.4</version> 
    <type>jar</type> 
    <scope>compile</scope> 
  </dependency> 
  <dependency> 
    <groupId>org.xwiki.platform</groupId> 
    <artifactId>xwiki-platform-rest-model</artifactId> 
    <version>7.2</version> 
  </dependency> 
</dependencies> 

java代碼示例

public void testNewPage() throws Exception{ 
    //定義對象 
    Page page =new Page(); 
    page.setTitle("testPage"); 
    page.setSyntax("xwiki/2.0"); 
    page.setContent("This is a testPage"); 
    page.setId("xwiki:Main.testPage"); 
    //初始化并轉(zhuǎn)換對象為xml文件的流 
    JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb"); 
    Marshaller marshaller=context.createMarshaller(); 
    Unmarshaller unmarshaller = context.createUnmarshaller(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    marshaller.marshal( page, out ); 
    //將流轉(zhuǎn)換并放入到InputStreamEntity中 
    InputStreamEntity inputStreamEntity=new InputStreamEntity(new ByteArrayInputStream(out.toByteArray())); 
 
    //發(fā)送請求 
    CloseableHttpClient httpclient = HttpClients.createDefault(); 
    HttpUriRequest httpPost = RequestBuilder.put() 
        .setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/testPage")) 
        .setEntity(inputStreamEntity) 
        .setHeader("Content-Type", "application/xml") 
        .setHeader("Cookie", cookieStr).build(); 
    //獲取返回結(jié)果 
    CloseableHttpResponse response = httpclient.execute(httpPost); 
    System.out.println(response.getStatusLine().getStatusCode()); 
    HttpEntity responseEntity=response.getEntity(); 
    System.out.println(responseEntity); 
    if(response.getStatusLine().getStatusCode()<400){ 
      Page responsePage = (Page) unmarshaller.unmarshal(responseEntity.getContent()); 
      System.out.println(responsePage); 
//      System.out.println(new Gson().toJson(responsePage)); 
    } 
  } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • 最全Gson使用

    最全Gson使用

    GSON彌補了JSON的許多不足的地方,在實際應(yīng)用中更加適用于Java開發(fā),本文主要介紹了最全Gson使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • springboot下使用shiro自定義filter的個人經(jīng)驗分享

    springboot下使用shiro自定義filter的個人經(jīng)驗分享

    這篇文章主要介紹了springboot下使用shiro自定義filter的個人經(jīng)驗,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java反射實現(xiàn)javabean轉(zhuǎn)json實例代碼

    java反射實現(xiàn)javabean轉(zhuǎn)json實例代碼

    基于java反射機制實現(xiàn)javabean轉(zhuǎn)json字符串實例,大家參考使用吧
    2013-12-12
  • Spring循環(huán)依賴的三種方式(推薦)

    Spring循環(huán)依賴的三種方式(推薦)

    本篇文章主要介紹了Spring循環(huán)依賴的三種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • spring boot啟動時mybatis報循環(huán)依賴的錯誤(推薦)

    spring boot啟動時mybatis報循環(huán)依賴的錯誤(推薦)

    今天小編抽時間給大家分享spring boot啟動時mybatis報循環(huán)依賴的錯誤,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-12-12
  • Java中CAS機制實現(xiàn)方法詳解

    Java中CAS機制實現(xiàn)方法詳解

    傳統(tǒng)的并發(fā)控制手段如synchronized和ReentrantLock雖有效防止資源競爭,卻可能引起性能開銷,相比之下,CAS(CompareAndSwap)提供一種輕量級的樂觀鎖策略,通過硬件級別的原子指令實現(xiàn)無鎖并發(fā),提高性能,需要的朋友可以參考下
    2024-09-09
  • 詳解在Java的Struts2框架中配置Action的方法

    詳解在Java的Struts2框架中配置Action的方法

    這篇文章主要介紹了詳解在Java的Struts2框架中配置Action的方法,講解了包括struts.xml中的action配置及基于注解方式Action配置的兩個方式,需要的朋友可以參考下
    2016-03-03
  • 一篇文章帶你搞懂Java restful 接口開發(fā)

    一篇文章帶你搞懂Java restful 接口開發(fā)

    這篇文章主要介紹了Java restful 接口開發(fā)的幾種方式(HTTPS),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • Java中避免過多if-else的幾種方法

    Java中避免過多if-else的幾種方法

    這篇文章主要介紹了Java中避免過多if-else的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 實現(xiàn)一個基于Servlet的hello world程序詳解步驟

    實現(xiàn)一個基于Servlet的hello world程序詳解步驟

    Java Servlet 是運行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來自 Web 瀏覽器或其他 HTTP 客戶端的請求和 HTTP 服務(wù)器上的數(shù)據(jù)庫或應(yīng)用程序之間的中間層
    2022-02-02

最新評論