cxf集成到spring中发布restful webservice
2015-11-06 00:00
489 查看
摘要: cxf集成到spring中发布restful webservice
一、maven依赖
二、web.xml配置
三、spring配置
四、接口类(实现类省略了):
一、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; }
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- android调用web service(cxf)实例应用详解
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- spring aop两种配置方式
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean