最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring整合CXF webservice restful實(shí)例詳解

 更新時(shí)間:2017年08月10日 10:52:15   作者:漢有游女,君子于役  
這篇文章主要為大家詳細(xì)介紹了Spring整合CXF webservice restful的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

webservice restful接口跟soap協(xié)議的接口實(shí)現(xiàn)大同小異,只是在提供服務(wù)的類(lèi)/接口的注解上存在差異,具體看下面的代碼,然后自己對(duì)比下就可以了。

用到的基礎(chǔ)類(lèi)

User.java

@XmlRootElement(name="User")
public class User {

  private String userName;
  private String sex;
  private int age;
  
  public User(String userName, String sex, int age) {
    super();
    this.userName = userName;
    this.sex = sex;
    this.age = age;
  }
  
  public User() {
    super();
  }

  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
  public static void main(String[] args) throws IOException {
    System.setProperty("http.proxySet", "true"); 

    System.setProperty("http.proxyHost", "192.168.1.20"); 

    System.setProperty("http.proxyPort", "8080");
    
    URL url = new URL("http://www.baidu.com"); 

    URLConnection con =url.openConnection(); 
    
    System.out.println(con);
  }
}

接下來(lái)是服務(wù)提供類(lèi),PhopuRestfulService.java

@Path("/phopuService")
public class PhopuRestfulService {


  Logger logger = Logger.getLogger(PhopuRestfulServiceImpl.class);

  @GET
  @Produces(MediaType.APPLICATION_JSON) //指定返回?cái)?shù)據(jù)的類(lèi)型 json字符串
  //@Consumes(MediaType.TEXT_PLAIN) //指定請(qǐng)求數(shù)據(jù)的類(lèi)型 文本字符串
  @Path("/getUser/{userId}")
  public User getUser(@PathParam("userId")String userId) {
    this.logger.info("Call getUser() method...."+userId);
    User user = new User();
    user.setUserName("中文");
    user.setAge(26);
    user.setSex("m");
    return user;
  }

  @POST
  @Produces(MediaType.APPLICATION_JSON) //指定返回?cái)?shù)據(jù)的類(lèi)型 json字符串
  //@Consumes(MediaType.TEXT_PLAIN) //指定請(qǐng)求數(shù)據(jù)的類(lèi)型 文本字符串
  @Path("/getUserPost")
  public User getUserPost(String userId) {
    this.logger.info("Call getUserPost() method...."+userId);
    User user = new User();
    user.setUserName("中文");
    user.setAge(26);
    user.setSex("m");
    return user;
  }
}

web.xml配置,跟soap協(xié)議的接口一樣

<!-- CXF webservice 配置 -->
  <servlet>
    <servlet-name>cxf-phopu</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf-phopu</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

Spring整合配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxws="http://cxf.apache.org/jaxws"
  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
  <import resource="classpath:/META-INF/cxf/cxf.xml" />
  <import resource="classpath:/META-INF/cxf/cxf-servlet.xml" />
  <import resource="classpath:/META-INF/cxf/cxf-extension-soap.xml" />

  <!-- 配置restful json 解析器 , 用CXF自帶的JSONProvider需要注意以下幾點(diǎn)
  -1、dropRootElement 默認(rèn)為false,則Json格式會(huì)將類(lèi)名作為第一個(gè)節(jié)點(diǎn),如{Customer:{"id":123,"name":"John"}},如果配置為true,則Json格式為{"id":123,"name":"John"}。
  -2、dropCollectionWrapperElement屬性默認(rèn)為false,則當(dāng)遇到Collection時(shí),Json會(huì)在集合中將容器中類(lèi)名作為一個(gè)節(jié)點(diǎn),比如{"Customer":{{"id":123,"name":"John"}}},而設(shè)置為false,則JSon格式為{{"id":123,"name":"John"}}
  -3、serializeAsArray屬性默認(rèn)為false,則當(dāng)遇到Collecion時(shí),格式為{{"id":123,"name":"John"}},如果設(shè)置為true,則格式為[{"id":123,"name":"john"}],而Gson等解析為后者
  
  <bean id="jsonProviders" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
    <property name="dropRootElement" value="true" />
    <property name="dropCollectionWrapperElement" value="true" />
    <property name="serializeAsArray" value="true" />
  </bean>
 -->
  <!-- 服務(wù)類(lèi) -->
  <bean id="phopuService" class="com.phopu.service.PhopuRestfulService" />
  <jaxrs:server id="service" address="/">
    <jaxrs:inInterceptors>
      <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    </jaxrs:inInterceptors>
    <!--serviceBeans:暴露的WebService服務(wù)類(lèi)-->
    <jaxrs:serviceBeans>
      <ref bean="phopuService" />
    </jaxrs:serviceBeans>
    <!--支持的協(xié)議-->
    <jaxrs:extensionMappings>
      <entry key="json" value="application/json"/>
      <entry key="xml" value="application/xml" />
      <entry key="text" value="text/plain" />
    </jaxrs:extensionMappings>
    <!--對(duì)象轉(zhuǎn)換-->
    <jaxrs:providers>
      <!-- <ref bean="jsonProviders" /> 這個(gè)地方直接用CXF的對(duì)象轉(zhuǎn)換器會(huì)存在問(wèn)題,當(dāng)接口發(fā)布,第一次訪問(wèn)沒(méi)問(wèn)題,但是在訪問(wèn)服務(wù)就會(huì)報(bào)錯(cuò),等后續(xù)在研究下 -->
      <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
    </jaxrs:providers>
  </jaxrs:server>
  
</beans>

客戶(hù)端調(diào)用示例:

對(duì)于get方式的服務(wù),直接在瀏覽器中輸入http://localhost:8080/phopu/services/phopuService/getUser/101010500 就可以直接看到返回的json字符串

{"userName":"中文","sex":"m","age":26} 

客戶(hù)端調(diào)用代碼如下:

public static void getWeatherPostTest() throws Exception{
    String url = "http://localhost:8080/phopu/services/phopuService/getUserPost";
    HttpClient httpClient = HttpClients.createSystem();
    //HttpGet httpGet = new HttpGet(url); //接口get請(qǐng)求,post not allowed
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader(CONTENT_TYPE_NAME, "text/plain");
    StringEntity se = new StringEntity("101010500");
    se.setContentType("text/plain");
    httpPost.setEntity(se);
    HttpResponse response = httpClient.execute(httpPost);

    int status = response.getStatusLine().getStatusCode();
    log.info("[接口返回狀態(tài)嗎] : " + status);

    String weatherInfo = ClientUtil.getReturnStr(response);

    log.info("[接口返回信息] : " + weatherInfo);
  }

客戶(hù)端調(diào)用返回信息如下:

ClientUtil類(lèi)是我自己封裝的一個(gè)讀取response返回信息的類(lèi),encoding是UTF-8

public static String getReturnStr(HttpResponse response) throws Exception {
    String result = null;
    BufferedInputStream buffer = new BufferedInputStream(response.getEntity().getContent());
    byte[] bytes = new byte[1024];
    int line = 0;
    StringBuilder builder = new StringBuilder();
    while ((line = buffer.read(bytes)) != -1) {
      builder.append(new String(bytes, 0, line, HTTP_SERVER_ENCODING));
    }
    result = builder.toString();
    return result;
  }

到這里,就介紹完了,大家手動(dòng)去操作一下吧,有問(wèn)題大家一塊交流。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java CompletableFuture使用方式

    Java CompletableFuture使用方式

    這篇文章主要介紹了Java CompletableFuture使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring的事務(wù)管理你了解嗎

    Spring的事務(wù)管理你了解嗎

    這篇文章主要為大家介紹了Spring的事務(wù)管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Java泛型之類(lèi)型擦除實(shí)例詳解

    Java泛型之類(lèi)型擦除實(shí)例詳解

    Java泛型在使用過(guò)程有諸多的問(wèn)題,如不存在List<String>.class,List<Integer>不能賦值給List<Number>(不可協(xié)變),奇怪的ClassCastException等,這篇文章主要給大家介紹了關(guān)于Java泛型之類(lèi)型擦除的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 精通Java List 按字段提取對(duì)象

    精通Java List 按字段提取對(duì)象

    這篇文章主要介紹了精通Java List 按字段提取對(duì)象的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Spring boot實(shí)現(xiàn)應(yīng)用打包部署的示例

    Spring boot實(shí)現(xiàn)應(yīng)用打包部署的示例

    本篇文章主要介紹了Spring boot實(shí)現(xiàn)應(yīng)用打包部署的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • 最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級(jí)應(yīng)用(附源碼)

    最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級(jí)應(yīng)用(附源碼)

    這篇文章主要介紹了最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級(jí)應(yīng)用(附源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java報(bào)錯(cuò)org.hibernate.TypeMismatchException的解決方法

    Java報(bào)錯(cuò)org.hibernate.TypeMismatchException的解決方法

    在Java開(kāi)發(fā)領(lǐng)域,尤其是涉及到數(shù)據(jù)持久化的項(xiàng)目中,Hibernate是一款廣泛使用的強(qiáng)大工具,然而,可能會(huì)在使用過(guò)程中遭遇各種報(bào)錯(cuò),其中org.hibernate.TypeMismatchException就是一個(gè)讓人頭疼的問(wèn)題,下面我們一起深入剖析這個(gè)報(bào)錯(cuò)信息
    2024-11-11
  • Sentinel熔斷規(guī)則原理示例詳解分析

    Sentinel熔斷規(guī)則原理示例詳解分析

    這篇文章主要介紹了Sentinel熔斷規(guī)則,采用了示例代碼的方式對(duì)Sentinel熔斷規(guī)則進(jìn)行了詳細(xì)的分析,以便廣大讀者朋友們更易理解,有需要的朋友可以參考下
    2021-09-09
  • Mybatis insert方法主鍵回填和自定義操作

    Mybatis insert方法主鍵回填和自定義操作

    這篇文章主要介紹了Mybatis insert方法主鍵回填和自定義操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java如何將可運(yùn)行jar打包成exe可執(zhí)行文件

    java如何將可運(yùn)行jar打包成exe可執(zhí)行文件

    Java程序完成以后,對(duì)于Windows操作系統(tǒng)習(xí)慣總是想雙擊某個(gè)exe文件就可以直接運(yùn)行程序,這篇文章主要給大家介紹了關(guān)于java如何將可運(yùn)行jar打包成exe可執(zhí)行文件的相關(guān)資料,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

新化县| 诸城市| 丹棱县| 开化县| 特克斯县| 富源县| 旌德县| 赤水市| 新丰县| 乡城县| 清镇市| 安丘市| 清水河县| 米泉市| 鄯善县| 新建县| 含山县| 报价| 汶川县| 会同县| 新民市| 永福县| 南召县| 彭阳县| 合山市| 平乡县| 于都县| 峨山| 红河县| 五指山市| 台山市| 北辰区| 资溪县| 昌乐县| 贵阳市| 库尔勒市| 绥化市| 阿拉尔市| 当阳市| 江永县| 岳阳市|