博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cxf集成到spring中发布restful webservice
阅读量:6971 次
发布时间:2019-06-27

本文共 5357 字,大约阅读时间需要 17 分钟。

hot3.png

一、maven依赖

        
            
org.apache.cxf
            
cxf-core
            
3.1.4
        
        
            
org.apache.cxf
            
cxf-rt-frontend-jaxws
            
3.1.4
        
        
            
org.apache.cxf
            
cxf-rt-frontend-jaxrs
            
3.1.4
        
        
            
org.apache.cxf
            
cxf-rt-transports-http
            
3.1.4
        
                
            
org.codehaus.jackson
             
jackson-jaxrs
            
1.9.13
        
        
            
com.alibaba
            
fastjson
            
1.2.4
        
               
            
org.apache.httpcomponents
            
httpclient
            
4.5.1
        
        
            
org.apache.httpcomponents
            
httpmime
            
4.5.1
        

二、web.xml配置

        
CXFServlet
        
org.apache.cxf.transport.servlet.CXFServlet
    
    
        
CXFServlet
        
/cxf/*
    

 

三、spring配置

 
Apache CXF resutful Web Service配置
 
  
 
      
  
   
  
  
         
         
     
     
      
     
   
        
     
  

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

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
 getJson(@QueryParam("param1")String param1, @QueryParam("param2")String param2);  @POST    @Path("/postJson")    @Produces({ MediaType.APPLICATION_JSON })    Map
 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 
 params=new ArrayList
();          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

你可能感兴趣的文章
流弊博客集锦(updating)
查看>>
dedecms 的这个dede:arclist里怎么调用全局变量?
查看>>
eclipse上跑项目越来越慢的解决办法
查看>>
个人开公司的流程,以后用得着(经典)(转)
查看>>
跟踪内核启动过程CONFIG_DEBUG_LL【转自】
查看>>
系统数据文件和信息
查看>>
Laravel 程序架构设计思路:使用动作类
查看>>
Java线程池实现原理与技术(ThreadPoolExecutor、Executors)
查看>>
Linux内核读书笔记第五周链接
查看>>
工厂模式(Factory Patter)
查看>>
痛苦的 01 矩阵(和式推导)
查看>>
我的Python成长之路---第三天---Python基础(11)---2016年1月16日(雾霾)
查看>>
intellij idea 用法
查看>>
Fabric项目学习总结
查看>>
【转】sqlserver临时表操作
查看>>
计算机等级考试成绩查询系统的设计与实现vfp
查看>>
android:exported 属性详解
查看>>
AT907 心配性な富豪、ファミリーレストランに行く。
查看>>
查询窗体的制作
查看>>
正则表达式详解<一>
查看>>