cxf集成到spring中发布restful webservice

一、maven依赖

<!--cxf-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.4</version>
        </dependency>
        
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
             <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.1</version>
        </dependency>

二、web.xml配置

<servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/cxf/*</url-pattern>
    </servlet-mapping>

 

三、spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 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.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"
 default-lazy-init="true">
 <description>Apache CXF resutful Web Service配置</description>
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 <bean id="webservice"  class="WebserviceImpl"/>
   
   <jaxrs:server id="cxfService" address="/ws">
  <jaxrs:serviceBeans>
   <ref bean="webservice" />
  </jaxrs:serviceBeans>
  <jaxrs:extensionMappings>
         <entry key="json" value="application/json" />
         <entry key="xml" value="application/xml" />
     </jaxrs:extensionMappings>
     <jaxrs:languageMappings>
      <entry key="en" value="en-gb"/>  
   </jaxrs:languageMappings>
   <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>  
   </jaxrs:providers>
 </jaxrs:server>
 
</beans>

四、接口类(实现类省略了):

package webservice;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/test")
public interface Webservice {
 @GET 
 @Path("/getText")
 @Produces({ MediaType.TEXT_PLAIN })
 String getText(@QueryParam("param1")String param1, @QueryParam("param2")String param2);
 
 @POST
    @Path("/postText")
    @Produces({ MediaType.TEXT_PLAIN })
    String postText(@FormParam("param1")String param1, @FormParam("param2")String param2);
 
 @GET
    @Path("/getJson")
    @Produces({ MediaType.APPLICATION_JSON })
 Map<String, Object> getJson(@QueryParam("param1")String param1, @QueryParam("param2")String param2);
 
 @POST
    @Path("/postJson")
    @Produces({ MediaType.APPLICATION_JSON })
    Map<String, Object> postJson(@FormParam("param1")String param1, @FormParam("param2")String param2);
}
五、测试代码:
/** 
     * 获取JAXRS WebService的结果信息 
     */  
    public static String postJAXRSWebService(String processURL){
        System.out.println( processURL);
        String result = "";
         try {  
          //创建一个HttpClient对象  
          CloseableHttpClient  httpclient = HttpClients.createDefault();
          //创建HttpPost对象  
          HttpPost  postRequest  =new HttpPost(processURL);  
           List <NameValuePair> params=new ArrayList<NameValuePair>();
          params.add(new BasicNameValuePair("param1","144203923"));
          params.add(new BasicNameValuePair("param2","中国"));
          //发出HTTP request
          UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(params, "UTF-8");
          postRequest.setEntity(paramEntity);
          System.out.println("------------------------------------------------------");
          System.out.println(paramEntity.getContentType());     
          System.out.println(paramEntity.getContentLength());     
          System.out.println(EntityUtils.getContentCharSet(paramEntity));     
          System.out.println(EntityUtils.toString(paramEntity));   
          System.out.println("--------------------------------------------------");          
          CloseableHttpResponse  response =httpclient.execute(postRequest);  
          //获取HttpEntity  
          HttpEntity entity=response.getEntity();  
          //获取响应的结果信息  
         result =EntityUtils.toString(entity);  
         
         } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
         
        return result;
    }  
    
    
    /** 
     * 获取JAXRS WebService的结果信息 
     */  
    public static String getJAXRSWebService(String processURL){
        System.out.println( processURL);
        String result = "";
         try {  
          //创建一个HttpClient对象  
             CloseableHttpClient  httpclient = HttpClients.createDefault();
          //创建HttpGet对象  
          HttpGet request=new HttpGet(processURL);  
         
          CloseableHttpResponse  response =httpclient.execute(request);  
        //获取HttpEntity  
        HttpEntity entity=response.getEntity();  
        //获取响应的结果信息  
         result =EntityUtils.toString(entity);  
         
         } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
         
        return result;
    }

转载于:https://my.oschina.net/u/216368/blog/526959

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值